Does this seem like the best/proper way to do backbone.js view inheritance

末鹿安然 提交于 2019-12-12 02:53:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!