Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is pushState for?

Tags:

backbone.js

I saw the latest backbone.js (0.5) introduced the pushState option for routing.

After reading through https://developer.mozilla.org/en/dom/manipulating_the_browser_history I have to say it is not very clear to me : what pushState is and what exactly it is that pushState brings, in the context of writing a web app with backbone; is it for :

  • improving urls: to have a 'real', bookmarkable, 'server-reachable' url, as opposed to hashes?

  • graceful degradation: allow the server to render correct pages without JS enabled ?

  • both/none of the above, or other reasons ?

Also, what am i doing wrong below ?:

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

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

new MyRouter

Backbone.history.start pushState: true

When I navigate to http://localhost#hello, the url is changed to http://localhost/#hello, but the callback is not fired ?

Thanks

like image 579
Running Turtle Avatar asked Jul 02 '11 10:07

Running Turtle


People also ask

What does the pushState () method do?

The pushState() method enables mapping of a state object to a URL. The address bar is updated to match the specified URL without actually loading the page.

Does history pushState refresh the page?

The history. pushState() method can be used to push a new entry into the browser's history—and as a result, update the displayed URL—without refreshing the page. It accepts three arguments: state , an object with some details about the URL or entry in the browser's history.

What is jquery pushState history?

The pushState() method is used to create a new history entry. You can store state, title, url. store data that is associated the new history entry. The state object will be used to store data that is associated with the new history entry.


1 Answers

You don't need the # prefix in your routes table. Try this:

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

As for pushState I think its both of the above. It does mean more work on the server-side than you would have to do with location hash because you are going to have to make sure that your server can serve pages for all those URLs.

like image 57
Steve Avatar answered Sep 30 '22 05:09

Steve