Javascript 将集合加载到backbone.js中的模型中
目标如下: 我有一个Javascript 将集合加载到backbone.js中的模型中,javascript,backbone.js,backbone-views,backbone-model,backbone.js-collections,Javascript,Backbone.js,Backbone Views,Backbone Model,Backbone.js Collections,目标如下: 我有一个家长模型。它位于一个名为parents的集合中。在父对象中是子对象的集合。因此,当我实例化父对象集合时,我得到了父对象模型,每个模型中都有子对象的集合 我尝试了几种不同的方法,我得到了不同的表现。有时孩子们根本不出现。有时,它们会在父级渲染循环的每次迭代中重复。我正在寻求帮助,了解这样做的最佳实践是什么 子项的列表不应更改,每次都应是相同的集合。我可以在以后的飞行中过滤差异 我已经把它简化为尽可能简单的数据提取,没有其他额外的内容来明确需要发生什么 我加载了两次子对象集合。(
家长
模型。它位于一个名为parents
的集合中。在父对象中
是子对象的集合
。因此,当我实例化父对象
集合时,我得到了父对象
模型,每个模型中都有子对象
的集合
我尝试了几种不同的方法,我得到了不同的表现。有时孩子们根本不出现。有时,它们会在父级渲染循环的每次迭代中重复。我正在寻求帮助,了解这样做的最佳实践是什么
子项的列表不应更改,每次都应是相同的集合。我可以在以后的飞行中过滤差异
我已经把它简化为尽可能简单的数据提取,没有其他额外的内容来明确需要发生什么
我加载了两次子对象
集合。(好吧,有很多次,但在父项
集合中有一次,在每个父项
模型中有一次)。这样,我就可以添加一个新的“父对象”,并可以访问子对象,以便在保存之前将它们添加到“父对象”模型中
问题:
- 如何确保将
子项
加载到父项
只有一次
- 如何将一个
子项
集合加载到父项
收藏
- 这样做对吗
型号:
$(function() {
window.Child = Backbone.Model.extend({});
window.Children = Backbone.Collection.extend({
model: Child
});
window.Parent = Backbone.Model.extend({
initialize: function() {
this.children = new Children();
children.fetch();
}
});
window.Parents = Backbone.Collection.extend({
model: Parent
initialize : function() {
this.childrenAll = new Children();
this.childrenAll.fetch();
}
});
// instantiate the collections
window.parents = new Parents();
});
我的看法:
$(function() {
window.ChildView = Backbone.View.extend({
className: "child-item",
template: _.template($('#child-template').html()),
initialize: function () {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
window.ChildrenView = Backbone.View.extend({
className: 'children',
template: _.template($('#children-template').html()),
initialize: function() {
_.bindAll(this, 'render');
this.collection.bind('reset', this.render);
},
render: function() {
this.$el.html(this.template());
this.collection.each(function(child) {
var view = new ChildView({ model:child });
$('.children-list').append(view.render().el);
});
return this;
}
});
window.ParentView = Backbone.View.extend({
className: "parent",
template: _.template($('#parent-template').html()),
initialize: function () {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
this.children = new Children({ collection: children });
$('.children-list').html(this.children.render().el);
return this;
}
});
window.ParentsView = Backbone.View.extend({
el: "#app",
template: _.template($('#parents-template').html()),
initialize: function () {
_.bindAll(this, 'render', 'add');
this.collection.bind('reset', this.render);
},
render: function() {
this.$el.html(this.template());
this.childrenView = new ChildrenView({ collection: children });
$('.children-new').append(this.childrenView.render().el);
this.collection.each(function(parent){
var view = new ParentView({ model: note });
$('#items-list').prepend(view.render().el);
});
return this;
}
});
});
路由器:
$(function() {
window.ParentsRouter = Backbone.Router.extend({
routes: {
'': 'list'
},
initialize: function() {
// starts by assigning the collection to a variable so that it can load the collection
this.parentsView = new ParentsView({
collection: parents
});
parents.fetch({ reset: true });
},
list: function () {
$('#app').empty();
$('#app').append(this.parentsView.render().el);
}
});
window.parentsRouter = new ParentsRouter();
Backbone.history.start();
});
事实证明,有几种不同的方法可以做到这一点。我选择在父模型的initialize方法中实例化和呈现子集合。问题是什么?我不敢相信我添加了所有这些,而且从未问过任何问题。问题已经添加。