2011-07-02 44 views
10

我看到最新的backbone.js(0.5)引入了路由的pushState選項。什麼是pushState?

通過https://developer.mozilla.org/en/dom/manipulating_the_browser_history 看完後我不得不說,這對我來說並不很清楚:什麼是pushState的和它到底是什麼是pushState的帶來,以書面方式向骨幹網絡應用程序的情況下;是它:

  • 改善網址:有一個「真正的」,可收藏,「服務器到達」的網址,而不是哈希?

  • 優雅的降級:允許服務器在沒有啓用JS的情況下呈現正確的頁面?

  • both/none以上,還是其他原因?

而且,我在做什麼錯了如下:?

class MyRouter extends Backbone.Router 
    routes : 
    ''  : 'index' 
    '#hello' :'hello' 

    index : -> console.log 'index' 
    hello: -> console.log 'hello' 

new MyRouter 

Backbone.history.start pushState: true 

當我瀏覽到http://localhost#hello,該URL被改變http://localhost/#hello,但回調不會觸發?

謝謝

回答

2

在路由表中不需要#前綴。試試這個:

routes : 
    ''  : 'index' 
    'hello' : 'hello' 

至於pushState,我認爲它的兩個以上。這意味着服務器端的更多工作要比使用位置哈希所做的更多工作,因爲您將不得不確保您的服務器可以爲所有這些URL提供頁面。