Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine hash and non-hash URLs in Backbone.js

Is there some way how to combine hash and non-hash URLs in Backbone.js application?

I set Backbone.history.start({pushState: true}). When user click on some link, I fetch JSON data from server, update page, and call Backbone.history.navigate to change URL in browser from (for example from example.com/zlinsky/kampan/mf/ to example.com/moravskoslezsky/kampan/mf/).
If user copy URL from browser and open in second tab, he will see same page (so every page updated this way have corresponding page on server). This is exactly what I want.

But now I have problem...

I have several <select> on page too. When user change value in them, I make some dynamic changes on page (without fetching JSON from server, updates is done only on client side). I would like change URLs according to <select>, for example to example.com/moravskoslezsky/kampan/mf/#state1 (so, when somebody send this URL, the other side will see same page, in same state as sender).

I could not find way, how to do it in Backbone.js. If I set pushState: true on Backbone.history, Router ignore hash tags.
If I set pushState: false, I am not able to set URLs like I describe in first paragraph above.

Thank you for any hint.

like image 417
msgre Avatar asked Jan 29 '12 18:01

msgre


Video Answer


1 Answers

You can call: Backbone.history.navigate( "/foo/bar#fragment" )

But i don't think it's a good idea, because ie doesn't support pushstate, so backbone is going to use hash bang urls (in ie).

Maybe you could use querystrings: Backbone.history.navigate( "/foo/bar?foo=bar", true ) which will be in modern browsers: http://domain.tld/foo/bar?foo=bar and in ie: http://domain.tld#/foo/bar?foo=bar

like image 82
oroce Avatar answered Oct 26 '22 09:10

oroce