Skip to content

default route #28

@snowkeeper

Description

@snowkeeper

I wanted a default route to run if there was not a match found. My route root can be defined in the module and is set on page render through bone.router.start({root:resp.path,pushState: true}). I was running into issues finding a simple work around.

My first question is am I overlooking something simple here?

If not, this is the solution I am using to add a catchall regex route. Is there a better way?

Add a defaultRoute as the last route:

'*': defaultRoute

and update routeToRegex:

routeToRegex = function(route) {
  var escapeRegExp, namedParam, optionalParam, splatParam;

  if(route === '*') {
    return new RegExp("([^\s]+)");
  } 

  optionalParam = /\((.*?)\)/g;
  namedParam = /(\(\?)?:\w+/g;
  splatParam = /\*\w+/g;
  escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g;
  route = route.replace(escapeRegExp, "\\$&").replace(optionalParam, "(?:$1)?").replace(namedParam, function(match, optional) {
    if (optional) {
      return match;
    } else {
      return "([^/]+)";
    }
  }).replace(splatParam, "(.*?)");
  return new RegExp("^" + route + "$");

};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions