backbone.js:如何向每个主干类添加自定义方法
我想向每个 Backbone 类添加一个自定义方法 - 模型、集合、路由器、视图。我怎样才能做到这一点?
这就是我到目前为止正在做的事情......
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 http://jsfiddle.net/fsFNW/
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要严格回答问题,请查看 http://jsfiddle.net/dira/bbnSE/
如果如果您使用它进行调试,我建议使用 window.debug 函数并使用更重要的消息(“获取”、“渲染”等),因为“我来到这里:模型”不是很有用。
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.