Last active
December 19, 2015 12:49
-
-
Save louy/5957306 to your computer and use it in GitHub Desktop.
Custom route function for backbone to allow before and after filters.
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
var AppRouter = Backbone.Router.extend({ | |
route: function(a,b,c) { | |
var self=this; | |
function _run(f) { | |
var g = Array.prototype.slice.apply(arguments, [1]); | |
if( self.before.apply(self, [a,g]) ) { | |
f.apply(self,g); | |
self.after.apply(self, [a,g]) | |
} | |
} | |
if( _.isFunction(c) ) { | |
c = _.wrap(c, _run); | |
} else if( _.isFunction(this[c]) ) { | |
c = _.wrap(this[c], _run); | |
} else if( _.isFunction(this[b]) ) { | |
b = _.wrap(this[b], _run); | |
} else { | |
b = _.wrap(b, _run); | |
} | |
Backbone.Router.prototype.route.apply(self, [a,b,c]); | |
}, | |
before: function(route) { | |
return true; | |
}, | |
after: function(route) { | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment