crossroads.js icon indicating copy to clipboard operation
crossroads.js copied to clipboard

Offer a way to check if a path matches any existing route

Open MaKleSoft opened this issue 10 years ago • 2 comments

In some scenarios I wanna now if crossroads.parse() has matched any routes. For example, if the path did not match any existing route, I might want to forward them to a default one. One way to provide this information would be to simply have parse() return true or false based on whether any route was matched. And/or you could provide an additional method that checks if a path matches any existing route without actually triggering the route callback if it does.

MaKleSoft avatar Feb 20 '15 10:02 MaKleSoft

when I need to forward to the default route I use the bypassed signal

also important to remind that signals are dispatched synchronously so you could implement your own method that returns true/false with something like:

var matched;

crossroads.bypassed.add(function() {
  matched = false;
});

function goAndCheck(path) {
  matched = true;
  crossroads.parse(path);
  return matched;
}

you can also use the internal method _getMatchedRoutes which should return an empty array if it can't match anything.

millermedeiros avatar Feb 24 '15 00:02 millermedeiros

@millermedeiros why don't you make the _getMatchedRoutes method available in the API documentation of crossroads?

I know that you use this function internally but there is no warranty that this would be maintain in the future or if it will be replaced by something else.

I believe it will be important to have documentation that this is possible. I am actually using this method but to figure it out I add to go through all crossroads.js to know its existence.

bzin avatar Oct 23 '15 09:10 bzin