如何在Backbone.js中使用PushState路由器

时间:2012-02-23 11:00:35

标签: model-view-controller html5 backbone.js router pushstate

我正在使用下面的路由器

App.Router.PersonRouter = Backbone.Router.extend({
    routes: {
        "": "homepage",
        "/p/:id": "getpost",
        "/p/:id/*file": "download"
    },
    homepage: function () {
        alert("requesting home page");
    },
    getpost: function (id) {
        alert("Requested post with id " + id);
    },
    download: function (id, file) {
        alert("person with id " + id + " is requesting file " + file);
    }
});

并开始在backbone.js中尝试HTML5 pushState选项。通过执行以下操作。因为文件是从index.html文件提供的。只是alerts在使用HTML5 pushstate api时没有显示,但在使用hashbang url时很高兴。

Backbone.history.start({pushState:true,root:"index.html"});

1 个答案:

答案 0 :(得分:4)

root中的Backbone.history.start选项不是资源。

另外,请确保您杀死路线中的前导/,因此"p/:id"代替"/p/:id" 这应该让你去。