Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
116 lines (88 sloc) 2.63 KB

Backbone.Detour Extra Features

Default Values

routeOptions: function() {
  this.optional('type', {default: 'manager'});
}

Now anytime type is not passed as a parameter to the route, the object passed to handleRoute will have a type property set to manager.

Clearing Paramaters

routeOptions: function() {
  this.optional('page', {default: '1'})
  this.optional('filter', {clears: 'page'})
  this.optional('order', {clears: 'page'})
}

Now if filter or order are set, it will reset page to 1.

Required Params

You can set a parameter as required by passing it to required instead of optional:

routeOptions: function() {
  this.required('owner');
}

Now if any params are set, but owner is not, then the object passed to handleRoute will be empty

Tokens

In Backbone.Detour all parameters are required to have a value. If you want to have a param without a value then list it as a token:

routeOptions: function() {
  this.tokens('debug','someOtherToken');
}

Now any tokens that you are expecting, if set in the route will be listed as a second argument to handleRoute:

handleRoute: function(args, toks) {
  if(toks.indexOf('debug') >= 0) {
    // do something
  }
}

Arrays

You can tell Backbone.Detour to treat a param as an array:

routeOptions: function() {
  this.optional('users', {type: 'array'});
}

Then when you update it, you pass an array of strings instead of just a single string:

router.updateRoute({users: ['jim', 'steve']});

Then the values passed to handleRoute will also be an array:

handleRoute: function(args) {
  console.log(args.users); // ['jim', 'steve']
}

You can also specify that you want new values to be automatically appended instead of replacing the array:

routeOptions: function() {
  this.optional('users', {type: 'array', append: true});
}

You can then specify a limit of how many values to store when appending:

routeOptions: function() {
  this.optional('users', {type: 'array', append: true, appendLimit: 2});
}

You can also specify that you want duplicated values to be cleared when appending:

routeOptions: function() {
  this.optional('users', {type: 'array', append: true, unique: true});
}

And finally, you can specify your own comparator for uniqueness:

routeOptions: function() {
  this.optional('users', {
    type: 'array', 
    append: true, 
    unique: true, 
    comparator: function(a,b){
      return a.substr(5) === b.substr(5);
    }
  });
}