2012-02-23 37 views
0

我使用下面如何使用pushState的路由器Backbone.js的

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"}); 

回答

4

Backbone.history.startroot選項是路徑而不是資源。

此外,請確保你殺死你的路線中的領先/,所以"p/:id"而不是"/p/:id" 這應該讓你去。

+0

我需要領先的'/'因爲url自然看起來'/#/ p/3'字符干擾了pushState api的工作嗎?你能解釋一下關於'root'文檔的更多內容,所以在使用root時,路由器是否匹配'index.html /#/ p/3'? – Deeptechtons 2012-02-24 04:54:08

+0

你不需要領先的'/'。所有路徑應該相對於你的根。你的服務器應該在/ root,/ root/p/3 /root/p/3/somefile.txt下正確回答。那麼它將與pushState一起工作或不一致。 – ggozad 2012-02-24 08:58:42

+0

現在用最笨的web服務器(IIS)是不可能的,你的服務器應該在/ root,/ root/p/3 /root/p/3/somefile.txt處作出適當的迴應,我們正處於使用微軟網絡技術的石器時代產品甚至現在 – Deeptechtons 2012-02-24 12:17:00