backbone.js:如何向每个主干类添加自定义方法
我想为每个主干类添加一个自定义方法—模型、集合、路由器、视图。我该怎么做 这就是我到目前为止正在做的事backbone.js:如何向每个主干类添加自定义方法,backbone.js,Backbone.js,我想为每个主干类添加一个自定义方法—模型、集合、路由器、视图。我该怎么做 这就是我到目前为止正在做的事 Backbone.Router.prototype.method1 = function() { console.log("I came here: router"); }; Backbone.View.prototype.method1 = function() { console.log("I came here: view"); }; Backbone.Model.pro
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");
};
我猜一定有更好、更优雅的方法来做到这一点
更新
下面是我最终实现它的方式。谢谢你关于登录@dira的建议
要严格回答问题,请查看 如果您使用它进行调试,我建议使用window.debug函数并使用更重要的消息(“抓取”、“渲染”等),因为“我来到这里:模型”不是很有用
window.debug_factory = function(kind) {
return function(message) {
console.log("I came here: " + kind + " " + " " + message);
}
};
Backbone.Model.prototype.debug = window.debug_factory('model');
Backbone.Collection.prototype.debug = window.debug_factory('collection');
Course = Backbone.Model.extend({});
Courses = Backbone.Collection.extend({model: Course});
c1 = new Course({name: 'c1'});
courses = new Courses();
courses.add(c1);
c1.debug('a');
courses.debug('b');
c1.debug('c');