Client side routing. How it works?

I need a client-side routing solution for working with a chrome application. I have researched a few and crossroads.js seems appropriate. When I include it in my html file, it does not seem to work; that is, if I use code like

crossroads.addRoute('/news/{id}', function(id){ alert(id); }); crossroads.parse('/news/123'); 

the page notifies '123', but if I type '/ news / 321' in the browser url line, it will transform the default action for the browser, rather than warning '321'. What am I doing wrong. (In addition, I understand that the name is broad, but I believe that the difficulties that I have with crossroads.js are more general than crossroads.js in particular. This is given as an example.)

+8
javascript client-side url-routing
source share
2 answers

Use Hasher (by the same author).

The documentation on the Crossroads page tells you that you need to use Hasher (because it will be used to monitor the widow.location panel.).

That way, you will also need to use Hasher and initialize it, then you can add Crossroads routes to Hasher to start monitoring for these specific routes.

 //setup crossroads crossroads.addRoute('foo'); crossroads.addRoute('lorem/ipsum'); crossroads.routed.add(console.log, console); //log all routes //setup hasher hasher.initialized.add(crossroads.parse, crossroads); //parse initial hash hasher.changed.add(crossroads.parse, crossroads); //parse hash changes hasher.init(); //start listening for history change //update URL fragment generating new history record hasher.setHash('lorem/ipsum'); 

http://millermedeiros.github.com/crossroads.js/

+8
source share

The parse command points to an intersection to look at a line and perform an action on it.

Therefore, in the case of crossroads.parse('/news/123'); he will always use /news/123 .

Since you want the crossroads to parse what you have in the address bar of the browser, you need to use this value in the analysis method:

 crossroads.parse(document.location.pathname); 
+6
source share

All Articles