为了更改漂亮urls上的哈希标签,我试图与backbone.js pushState打交道。使用localhost和我的脚本的路径是http://localhost/test/backbone/test.html。但是每次点击都会把我扔到localhost/login上。我做错了什么?
var AppRouter = Backbone.Router.extend({
routes: {
"login": "getPost",
"*actions": "defaultRoute"
}
});
var app_router = new AppRouter;
app_router.on('route:getPost', function (id) {
alert( "login" );
});
app_router.on('route:defaultRoute', function (actions) {
alert( actions );
});
app_router.navigate("/login", {trigger: true});
Backbone.history.start({pushState: true, root: '/login/'});发布于 2014-04-22 12:52:47
你需要添加:
$(document).delegate("a", "click", function(evt) {
var href = $(this).attr("href");
var protocol = this.protocol + "//";
if (href.slice(protocol.length) !== protocol && protocol !== 'javascript://' && href.substring(0, 1) !== '#') {
evt.preventDefault();
Backbone.history.navigate(href, true);
}
});最后两个字符串应该是:
app_router.navigate("/login", {trigger: true}); // <- should remove this string
Backbone.history.start({pushState: true, root:"test/backbone/test.html"});https://stackoverflow.com/questions/23218417
复制相似问题