什么是pushState?

Run*_*tle 10 backbone.js

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

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

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

  • 优雅降级:允许服务器在没有启用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
Run Code Online (Sandbox Code Playgroud)

当我导航到http:// localhost#hello时,url会更改为http:// localhost /#hello,但是不会触发回调?

谢谢

Ste*_*eve 2

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

  routes :
    ''       : 'index'
    'hello'  : 'hello'
Run Code Online (Sandbox Code Playgroud)

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