Skip to content
millermedeiros edited this page May 4, 2011 · 17 revisions

Read the documentation for a detailed explanation of available methods and features.

simple usage

crossroads.addRoute('/news/{id}', function(id){
  alert(id);
});
crossroads.parse('/news/123'); //will match '/news/{id}' route passing 123 as param

storing route reference and attaching mutiple listeners

var articleRoute = crossroads.addRoute('/article/{category}/{name}');
articleRoute.matched.add(function(category, name){
  alert(category);
});
articleRoute.matched.add(function(category, name){
  alert(name);
});
crossroads.parse('/article/lol_catz/keyboard_cat'); //will match articleRoute passing "lol_catz" and "keyboard_cat" as param

using RegExp to validate params

var specialNews = crossroads.addRoute('/news/{id}');
specialNews.matched.add(function(id){
  alert(id);
});
specialNews.rules = {
  id : /[0-9]+/ //match only numeric ids
};
crossroads.parse('/news/asd'); //won't match since ID isn't numeric
crossroads.parse('/news/5'); //will match `specialNews` and pass 5 as param to all listeners

using functions to validate params

var specialNews = crossroads.addRoute('/news/{id}');
specialNews.matched.add(function(id){
  alert(id);
});
specialNews.rules = {
  id : function(value, request, matches){
    return value === 'asd';
  }
};
crossroads.parse('/news/asd'); //will match
crossroads.parse('/news/5'); //won't match

using arrays to validate params

var specialNews = crossroads.addRoute('/news/{id}');
specialNews.matched.add(function(id){
  alert(id);
});
specialNews.rules = {
  id : ['asd', 5, 123, 23456, 'qwerty']
};
crossroads.parse('/news/asd'); //will match
crossroads.parse('/news/5'); //will match
crossroads.parse('/news/loremipsum'); //won't match

manually checking if route match a string

var myRoute = crossroads.addRoute('/foo/{id}');
var match = myRoute.match('/foo/bar'); //true

disposing route

var myRoute = crossroads.addRoute('/foo/{id}');
myRoute.dispose(); //remove route from crossroads and also remove all listeners from route.matched
Clone this wiki locally