【发布时间】:2019-07-04 19:56:58
【问题描述】:
我在带有嵌套集合的主干js 中面临一些架构问题。我有模型集合,每个模型内部都可以包含与儿童相同的模型集合。所以在父视图渲染中,我正在渲染子集合并将其 dom 附加到父视图。它工作得很好。
但是在父模型更改时,我正在重新渲染导致其子模型再次渲染的视图。但是当父母模型发生变化时,我不会创建儿童 dom,因为子集合没有变化。我可以在不重新渲染的情况下使用儿童 dom。有什么方法可以实现吗?
家长查看代码:
RowView.prototype.initialize = function(options) {
this.childCollection = options.childCollection;
this.listenTo(this.model, "change", this.render);
}
RowView.prototype.render = function() {
var existingControls = this.model.get("controls").toJSON();
this.childCollection = this.model.get("controls");
this.childCollection.bind('add', this.addOneChild, this);
this.childCollection.bind('reset', this.resetChildrens, this);
this.childCollection.reset(existingControls, this);
};
RowView.prototype.addOneChild = function(respModel) {
view = new RowViewChildView({
model: respModel,
parentView: this
});
this.$el.find('.childrens').append(view.render().el);
};
RowView.prototype.resetChildrens = function(currentCollection) {
this.$el.find(".childrens").html('');
return this.addAllChildrens();
};
RowView.prototype.addAllChildrens = function() {
return this.childCollection.each(this.addOneChild, this);
};
我也不想放弃子视图事件。
提前致谢
【问题讨论】:
标签: backbone.js