什么是pushState?

时间:2011-07-02 10:29:58

标签: backbone.js

我看到最新的backbone.js(0.5)引入了用于路由的pushState选项。

阅读https://developer.mozilla.org/en/dom/manipulating_the_browser_history后 我不得不说对我来说不太清楚:pushState是什么以及pushState带来了什么,在编写带有主干的Web应用程序的情况下;是为了:

  • 改进网址:拥有一个“真实的”,可收藏的,“服务器可访问的”网址,而不是哈希?

  • 优雅降级:允许服务器在没有启用JS的情况下呈现正确的页面?

  • 以上都没有或其他原因?

另外,我在下面做错了什么?:

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时,网址会更改为http://localhost/#hello,但回调未被触发?

由于

1 个答案:

答案 0 :(得分:2)

您的路线表中不需要#前缀。试试这个:

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

至于pushState,我认为它都是上述两种。它确实意味着在服务器端的工作比使用位置哈希更多,因为您必须确保您的服务器可以为所有这些URL提供页面。