简体   繁体   English

Backbone View是否总是需要Backbone模型?

[英]Does a Backbone View always require a Backbone Model?

I am learning Backbone. 我正在学习Backbone。

I am wondering whether or not a Backbone View always requires a Backbone Model. 我想知道Backbone View是否总是需要Backbone Model。

For example, let's say I have a panel that contains two child panels. 例如,假设我有一个包含两个子面板的面板。 The way I would structure this is with a parent view for the main panel, then two child views for the child panels... 我构建它的方式是使用主面板的父视图,然后是子面板的两个子视图......

    var OuterPanel = Backbone.View.extend({
        initialize: function() {
            this.innerPanelA = new InnerPanelA(innerPanelAModel);
            this.innerPanelB = new InnerPanelB(innerPanelBModel);
        },
    });

    var outerPanel = new OuterPanel();

The parent view is really just a container. 父视图实际上只是一个容器。 It may have some controls in it, but no data that needs to be persisted. 它可能有一些控件,但没有需要持久化的数据。 Is this the proper way to do it? 这是正确的方法吗? Or is this bad practice? 或者这是不好的做法?

Thnx (in advance) for your help Thnx(提前)为您提供帮助

As said in Backbone.View docs Backbone.View文档中所述

Backbone views are almost more convention than they are code — they don't determine anything about your HTML or CSS for you, and can be used with any JavaScript templating library. 骨干视图几乎比代码更常见 - 它们不会为您确定HTML或CSS的任何内容,并且可以与任何JavaScript模板库一起使用。

In other words, if you don't have a model, don't use a model. 换句话说,如果您没有模型,请不要使用模型。 On the other hand, I would inject the children models as options to the outer view instance and not rely on global variables, something like this: 另一方面,我会将子模型注入外部视图实例的选项而不依赖于全局变量,如下所示:

var OuterPanel = Backbone.View.extend({
    initialize: function(options) {
        this.innerPanelA = new InnerPanelA({model: options.modelA});
        this.innerPanelB = new InnerPanelB({model: options.modelB});
    }
});

var outerPanel = new OuterPanel({
    modelA: innerPanelAModel,
    modelB: innerPanelBModel
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM