Skip to content

Instantly share code, notes, and snippets.

@datapimp
Created March 30, 2011 17:21
Show Gist options
  • Save datapimp/894830 to your computer and use it in GitHub Desktop.
Save datapimp/894830 to your computer and use it in GitHub Desktop.
Before / After filters for backbone controllers
_.extend(Backbone.Controller.prototype, Backbone.Events, {
route : function(route, name, callback) {
Backbone.history || (Backbone.history = new Backbone.History);
if (!_.isRegExp(route)) route = this._routeToRegExp(route);
Backbone.history.route(route, _.bind(function(fragment) {
var args = this._extractParameters(route, fragment);
if( _.isFunction( this.before ) ){
this.before.apply(this, args)
}
callback.apply(this, args);
if( _.isFunction( this.after ) ){
this.after.apply(this, args)
}
this.trigger.apply(this, ['route:' + name].concat(args));
}, this));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment