Backbone pushState路由

时间:2014-04-22 11:39:04

标签: javascript backbone.js pushstate

我正在尝试使用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/'});

1 个答案:

答案 0 :(得分:3)

您需要添加:

$(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);
    }
});

最后2个字符串应为:

app_router.navigate("/login", {trigger: true}); // <- should remove this string
Backbone.history.start({pushState: true, root:"test/backbone/test.html"});
相关问题