Created
June 7, 2014 21:27
-
-
Save jaridmargolin/13aa6394096006b5e6f0 to your computer and use it in GitHub Desktop.
underscore.js decorator method
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
/*! | |
* utils.js | |
* | |
* Copyright (c) 2014 | |
*/ | |
define([ | |
'underscore', | |
],function (_) { | |
// ---------------------------------------------------------------------------- | |
// Mixin | |
// ---------------------------------------------------------------------------- | |
_.mixin({ | |
// | |
// Wrapper around _.wrap to allow a chain of methods | |
// to be applied before a fn. Acts similiar to python | |
// decorators. Great for use on route methods (auth, | |
// argument checking, etc...) | |
// | |
decorate: function() { | |
// Start at first decorator and work backwards | |
var i = arguments.length, | |
fn; | |
while(i-- > 1) { | |
fn = this.wrap(fn ? fn : arguments[i], arguments[i - 1]); | |
} | |
return fn; | |
} | |
}); | |
// ---------------------------------------------------------------------------- | |
// Expose | |
// ---------------------------------------------------------------------------- | |
return _; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment