Created
March 30, 2011 17:21
-
-
Save datapimp/894830 to your computer and use it in GitHub Desktop.
Before / After filters for backbone controllers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_.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