Skip to content

Instantly share code, notes, and snippets.

@SinisterMinister
Last active January 4, 2016 01:29
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 SinisterMinister/8548780 to your computer and use it in GitHub Desktop.
Save SinisterMinister/8548780 to your computer and use it in GitHub Desktop.
/**
* Decorates the model with a new method that has access to original methods
* @param model
* @param name
* @param method
*/
function decorateModel (model, name, method) {
var originalMethod = model[name],
decorator = function () {
var returnVal;
// Check to see if there is an original method. If so, make the original method visible to the decorator
if (originalMethod !== undefined) {
// Do some awkward hot potato shit with the functions so the decorator has access to the original method
model[name] = originalMethod;
// Fire off the method with the proper context
returnVal = method.apply(model, arguments);
// More hot potato to put things back to normal
model[name] = decorator;
return returnVal;
}
// Since there's nothing to override, no funny business
return method.apply(model, arguments);
};
model[name] = decorator;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment