在骨干网做鉴于混入有道 [英] Proper way of doing view mixins in Backbone
问题描述
我延长基地骨干认为所有的时间,并且每个部分基本观点,这样我可以在多个层次上延伸。我的问题是,什么是做视图混入的最有效的方法:可重复使用的视图的谐音,可以在任何视图混合。例如:
I extend base backbone views all the time and have a base view per section so that I can extend on multiple levels. My question is, what's the most effective way of doing view mixins: reusable view partials that can be mixed in to any view. For example:
var BaseProfile = Backbone.View.extend({ ...});
var UserProfile = BaseProfile.extend({ ...});
var VideoSupport = Backbone.View.extend({ ...});
什么是混入 VideoSupport
视图(事件对象和几个方法)与用户配置
视图的最佳方法是什么?
What's the best way to mixin VideoSupport
view (an event object and a few methods) with UserProfile
view?
推荐答案
该underscore.js库提供了一个延长
方法你想要做什么。您可以在任何物体上定义的功能,然后毫不夸张地复制和放大器;粘贴所有的方法,并从该对象属性到另一个
The underscore.js library provides an extend
method that does what you want. You can define functionality on any object, and then quite literally copy & paste all of the methods and attributes from that object to another.
骨干延长
的意见,模型和路由器的方法是围绕下划线的一个包装延长
。
Backbone's extend
methods on Views, Models, and Routers are a wrapper around underscore's extend
.
var MyMixin = {
foo: "bar",
sayFoo: function(){alert(this.foo);}
}
var MyView = Backbone.View.extend({
// ...
});
_.extend(MyView.prototype, MyMixin);
myView = new MyView();
myView.sayFoo(); //=> "bar"
这篇关于在骨干网做鉴于混入有道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!