Client side routing. How does it work?

折月煮酒 提交于 2019-12-03 07:21:05

Use Hasher (by the same author) also.

The documentation on the Crossroads page tells you that you need to use Hasher, (because that will be used for monitoring the widow.location bar.).

So you would also need to use Hasher, and initialise it, then you can add your "Crossroads" routes to Hasher to start monitoring for those particular 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/

The command parse tells crossroads to have a look at the string and do an action based on it.

So in the case of crossroads.parse('/news/123'); it will always use /news/123.

Since you want crossroads to parse what you have in the browser address bar, you'll need to use that value in the parse method:

crossroads.parse(document.location.pathname);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!