这似乎是最好的/正确的方式做Backbone.js的继承查看 [英] Does this seem like the best/proper way to do backbone.js view inheritance
问题描述
这是有点这个问题的后续行动,我问
this is sort of a follow up of this question that i asked
但它得到那种混乱的,现在我有一个解决方案,我只想有人告诉我,如果我疯了。
but it got kind of messy and now I have a solution I would just like someone to tell me if I'm crazy
我在做什么如下:
dci.Controller = Backbone.View.extend({
defaults:function(){
return {
views:{},
modules:{},
isDestroyed:false,
destroy:function(){
if(this.isDestroyed){return;}
this.isDestroyed = true;
$.each(this.views,function(i,item){
item.remove();
});
$.each(this.modules,function(i,item){
item.destroy();
});
}
}
}
});
然后,我这样做是为了使用控制器:
then I do this to use the controller:
dci.AssetController = dci.Controller.extend({
events:_.extend({
// whatever events here
}, dci.Controller.prototype.events),
initialize:function(options){
$.extend(this,this.defaults());
}
});
我终于实例如下:
finally i instantiate as follows:
var controller = new dci.AssetController(someOptions);
所以现在我的控制器变种我有意见和模块是每个实例是唯一的,所以如果我有两个dci.AssetCotrollers出于某种原因(其实我使用的模块相同的模型集合,这是当你有同一类的多个实例),他们每一个,有自己的看法和模块集合。
so now on my controller var I have a collection of views and modules that are unique per instance so if I have two dci.AssetCotrollers for some reason ( actually I'm using the same model for modules and this is when you have multiple instances of the same class ) they each have there own collection of views and modules.
这害了我一段时间,但我认为这会工作,我特别要感谢布赖恩Genisio谁让我喜欢提到这条赛道在上面的。
this was killing me for a while but I think this will work and I particularly would like to thank Brian Genisio who got me on this track in the above mentioned like.
现在请告诉我,如果这是一个疯狂的模式。
谢谢,
RAIF
Now please tell me if this is a crazy pattern. Thanks, Raif
推荐答案
那么,如果你正在创建一个新的基本视图,你应该有景术语坚持,而不是为previously在后端控制器..这可能会混淆有人。
Well if you are creating a new base view, you should probably stick with View terminology, not Controllers that were previously in backend.. that might confuse someone.
其次,我不知道你是如何设置 isDestroyed
。
Secondly, I don't see how you are setting isDestroyed
.
在一般情况下,使得分层的观点听起来不错(我一直在做手工到目前为止)。不知道你是如何使用的模块变量,但。否则,非包装的看法,我认为这是太多额外的初始化code。
In general, making hierarchical views sounds good to me (i kept doing it manually so far). Not sure how you use modules variable though. Otherwise, in non-wrapper views I think that is too much extra initialization code.
这篇关于这似乎是最好的/正确的方式做Backbone.js的继承查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!