Last active
December 15, 2015 11:59
-
-
Save dmitry-vsl/5257024 to your computer and use it in GitHub Desktop.
Backbone monkey patch to automatically trace all model method invocations
This file contains hidden or 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
/* | |
Monkey-patches Backbone in so way that every model method logs its invocation | |
*/ | |
var mdl = Backbone.Model; | |
var ext = Backbone.Model.extend; | |
Backbone.Model.extend = function(attributes, options) { | |
for(var attr in attributes){ | |
if(attributes.hasOwnProperty(attr) && typeof(attributes[attr]) == 'function'){ | |
(function(func){ | |
attributes[attr] = function(){ | |
console.log('invoked',attr); | |
return func.apply(this,arguments) | |
}; | |
})(attributes[attr]); | |
} | |
} | |
return ext.call(mdl,attributes,options); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment