我正在学习 Backbone。
我想知道主干 View 是否总是需要主干模型。
例如,假设我有一个包含两个子面板的面板。我构建它的方式是使用主面板的父 View ,然后是子面板的两个 subview ...
var OuterPanel = Backbone.View.extend({
initialize: function() {
this.innerPanelA = new InnerPanelA(innerPanelAModel);
this.innerPanelB = new InnerPanelB(innerPanelBModel);
},
});
var outerPanel = new OuterPanel();
父 View 实际上只是一个容器。它可能有一些控件,但没有需要持久化的数据。这是正确的方法吗?或者这是不好的做法?
感谢(提前)您的帮助
最佳答案
如 Backbone.View docs 中所述
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.
换句话说,如果你没有模型,就不要使用模型。另一方面,我会将子模型作为外部 View 实例的选项注入(inject),而不依赖于全局变量,如下所示:
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
});
关于javascript - Backbone View 是否总是需要 Backbone 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13707944/