Skip to content

Instantly share code, notes, and snippets.

@jaridmargolin
Created June 7, 2014 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaridmargolin/13aa6394096006b5e6f0 to your computer and use it in GitHub Desktop.
Save jaridmargolin/13aa6394096006b5e6f0 to your computer and use it in GitHub Desktop.
underscore.js decorator method
/*!
* 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