Last active
January 4, 2016 01:29
-
-
Save SinisterMinister/8548780 to your computer and use it in GitHub Desktop.
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
/** | |
* 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