A different kind of router
Normal routing in Backbone.js works great for hierarchical routes (e.g. when you need to drill down to your content.) However it makes it very cumbersome to use as a parameter store.
Lets say you have 3 possible parameters you are listening for: type, date, and owner; and any combination could be present. The normal way to define your routes would be:
routes:"" : "someFunction""type/:type" : "someFunction""date/:date" : "someFunction""owner/:owner" : "someFunction""type/:type/date/:date" : "someFunction""date/:date/owner/:owner" : "someFunction""type/:type/owner/:owner" : "someFunction""type/:type/date/:date/owner/:owner" : "someFunction"
And this requires the order to always be the same. Add a fourth parameter and gets really long.
With Backbone.Detour the definition would simply be:
You extend Backbone.Detour and define the methods
handleRoute like so:
var router = BackboneDetour;
You specify what parameters you are expecting in routeOptions then any time a route change is triggered, any passed parameters that you are expecting will be available as properties in the object passed to handleRoute.
Ok, so lets say you want to update the route and change the value for owner. Using the normal:
would result in whatever you prevously had set for type and date being cleared. So you would have to keep track of all the values for all the paramters you are not changing, just so you could not change them:
For long lists of parameters this would be tedious.
With Backbone.Detour you just specify what you want to change:
Everything else in the route would stay the same. But what if I want to clear a parameter? Easy, just set it to false:
The above call would update the route to change the owner param to whoever and clear any value for the type parameter if it exists.
Add specs for any new functionality. To run the specs:
npm install -g jasmine-node
npm install -g coffee-script
jasmine-node --coffee spec/