I want to add a custom method to each of the Backbone classes – model, collection, router, view. How can I do that?
Here is what I am doing till now….
Backbone.Router.prototype.method1 = function() {
console.log("I came here: router");
};
Backbone.View.prototype.method1 = function() {
console.log("I came here: view");
};
Backbone.Model.prototype.method1 = function() {
console.log("I came here: model");
};
Backbone.Collection.prototype.method1 = function() {
console.log("I came here: collection");
};
I am guessing there must be a better and more elegant way to do this?
Update
Here’s how I implemented it finally. Thanks for the advice about logging @dira
To strictly reply to the question, check out http://jsfiddle.net/dira/bbnSE/
If you are using this for debugging, I recommend having a window.debug function and using more significant messages (“fetching”, “rendering” etc) as “I came here: model” is not very useful.