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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With