Object Based Routes

HashRouter takes advantage of javascript object notation for designing routes that are easier to read and write than a regular expression. (Ok, that's subjective but we think they are.)

Perhaps you are using HashRouter for a site on baking. You've got a number of variables that you want the url to manage.

Urls without routes would look like:

Adding routes cleans them up considerably:

Here is the route for this site:

var routes = { mode : { list : { sort : { _ : { direction : {} } } }, edit : { recipeID : {} }, view : { recipeID : { _ : { pictures : {} } } } } };

The 'mode' variable is the first one in the url, and is therefore the root level item in our object. It has three possible values: list, edit, and view. When the route decoder finds a match it moves into that part of the object tree.

The routes use an underscore _ to match any value. In this example, the sort column can be any value, and the next variable will still be direction.

The decoder is smart enough to back up if it finds that your route contained variables it couldn't parse. It will then continue down the route tree trying to find a match.

View this route in action.