javascript - Crossroadsjs routing: how to use it? - Stack Overflow

I am facing the same problem as this one as I am trying to figure out how to use crossroads for a few h

I am facing the same problem as this one as I am trying to figure out how to use crossroads for a few hours now and nothing seems to work. its webiste is just another poor documented site... I think I am probably daft! I wonder if anyone has made it?

html head,

<title>Crossroads</title>
  <script src="js/libs/signals.js"></script>
  <script src="js/libs/crossroads.min.js"></script>
  <script src="js/app.js"></script>
</head>

app.js, just as simple as this,

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

so I try it out on my localhost browser,

http://localhost/crossroadjs/#/news/123

nothing happens. I thought it would be 123??

I am facing the same problem as this one as I am trying to figure out how to use crossroads for a few hours now and nothing seems to work. its webiste is just another poor documented site... I think I am probably daft! I wonder if anyone has made it?

html head,

<title>Crossroads</title>
  <script src="js/libs/signals.js"></script>
  <script src="js/libs/crossroads.min.js"></script>
  <script src="js/app.js"></script>
</head>

app.js, just as simple as this,

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

so I try it out on my localhost browser,

http://localhost/crossroadjs/#/news/123

nothing happens. I thought it would be 123??

Share Improve this question edited May 23, 2017 at 12:24 CommunityBot 11 silver badge asked Jan 3, 2014 at 16:45 RunRun 57.4k178 gold badges464 silver badges771 bronze badges 1
  • If you've checked examples for reference? github./millermedeiros/crossroads.js/wiki/Examples – moka Commented Jan 3, 2014 at 17:12
Add a ment  | 

2 Answers 2

Reset to default 4

Crossroads doesn't handle history/state change events from the browser. From their site:

A routes system shouldn't do anything else besides routing.

Instead, the site remends Hasher for this purpose and gives a rather plete looking example:

//setup crossroads
crossroads.addRoute('foo');
crossroads.addRoute('lorem/ipsum');
crossroads.routed.add(console.log, console); //log all routes

//setup hasher
function parseHash(newHash, oldHash){
  crossroads.parse(newHash);
}
hasher.initialized.add(parseHash); //parse initial hash
hasher.changed.add(parseHash); //parse hash changes
hasher.init(); //start listening for history change

//update URL fragment generating new history record
hasher.setHash('lorem/ipsum');

Alternatively you could use a different history plugin, or write something yourself. But crossroads leaves that part up to you.

Crossroads.js gives crossroads.addRoute(pattern, [handler], [priority]); API to add route patterns. However, when the first time you load the page Crossroads does not automatically initiate the parser to check against the url of the page. You need to add crossroads.parse(document.location.pathname); on you document load to trigger the route. Check out Crossroads.js Tutorial.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745662452a4638919.html

相关推荐

  • javascript - Crossroadsjs routing: how to use it? - Stack Overflow

    I am facing the same problem as this one as I am trying to figure out how to use crossroads for a few h

    22天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信