问题
this is sort of a follow up of this question that i asked
Backbone.js Inherit Views
but it got kind of messy and now I have a solution I would just like someone to tell me if I'm crazy
what I'm doing is as follows:
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);
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.
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.
Now please tell me if this is a crazy pattern. Thanks, Raif
回答1:
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.
Secondly, I don't see how you are setting isDestroyed
.
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.
来源:https://stackoverflow.com/questions/7838926/does-this-seem-like-the-best-proper-way-to-do-backbone-js-view-inheritance