-
Notifications
You must be signed in to change notification settings - Fork 155
Examples
millermedeiros edited this page May 4, 2011
·
17 revisions
Read the documentation for a detailed explanation of available methods and features.
crossroads.addRoute('/news/{id}', function(id){
alert(id);
});
crossroads.parse('/news/123'); //will match '/news/{id}' route passing 123 as param
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
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
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
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
var myRoute = crossroads.addRoute('/foo/{id}');
var match = myRoute.match('/foo/bar'); //true
var myRoute = crossroads.addRoute('/foo/{id}');
myRoute.dispose(); //remove route from crossroads and also remove all listeners from route.matched