Skip to content

Instantly share code, notes, and snippets.

@dmitry-vsl
Last active December 15, 2015 11:59
Show Gist options
  • Save dmitry-vsl/5257024 to your computer and use it in GitHub Desktop.
Save dmitry-vsl/5257024 to your computer and use it in GitHub Desktop.
Backbone monkey patch to automatically trace all model method invocations
/*
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