我正在尝试处理 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/'});
最佳答案
你需要添加:
$(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"});
关于javascript - 主干 pushState 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23218417/