Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execution order of Matched Routes Question #13

Closed
mhemesath opened this issue May 13, 2011 · 4 comments
Closed

Execution order of Matched Routes Question #13

mhemesath opened this issue May 13, 2011 · 4 comments

Comments

@mhemesath
Copy link

In your example below,

  • Is the execution order of the matches the same as they are added?
  • Do they execute sync or async.
  • Is there any way to cancel the sequential matches from firing? Example.. if the first match checks to see if a user is authenticated, if not redirect rather than executing all sequential requests.

var route1 = crossroads.addRoute('/news/{id}');
route1.matched.add(function(id){
  console.log('handler 1: '+ id);
});
route1.matched.add(function(id){
  console.log('handler 2: '+ id);
});
crossroads.parse('/news/123'); //will trigger both handlers of `route1`
@millermedeiros
Copy link
Owner

the execution is based on the matched.add order or can be specified by setting the matched signal listener priority (1st example of advanced features) - you can stop the dispatch by calling route1.matched.halt() or by simply returning false on the signal handler (Stop/Halt Propagation examples on same link).

you can use a function to validate the route if you need to check something before dispatching the matched signal:

var route1 = crossroads.addRoute('/news/{id}');
route1.rules = {
  id : function(value, request, valuesObj){
    return isUserAuthenticated(); //if false it won't match route
  }
};

using the function to validate is probably the recommended approach since you can't ensure a specific listener will always be the first one to be called (you can add another listener with a higher priority later that will get executed before)

currently the rules object doesn't have any "magic" property that validates the whole route, maybe I should add it for cases where you don't have any "{variable}" but still need to do some kind of validation based on the application estate.. - I created a new issue (#14) for it.

you can find more info about how to use the Route.rules on the documentation.

cheers.

@mhemesath
Copy link
Author

Thanks man, this looks awesome and is exactly what I need! I saw it in JavaScript Weekly. Good work! Integrating into my project.... now!

@mhemesath
Copy link
Author

Could you validate off the section? Seems like a feasible interim solution until you get your magic property in.

var route1 = crossroads.addRoute('/{section}/{id}');
route1.rules = {
  section : function(value, request, valuesObj){
    return isUserAuthenticated() ? ["news"] : []
 }
};

@millermedeiros
Copy link
Owner

the validation function should return true or false.

route1.rules = {
  section : function(value, request, valuesObj){
    return (isUserAuthenticated() && value == "news");
 }
};

example above will validate only if user is authenticated and section fragment is "news".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants