【发布时间】:2012-09-29 19:46:30
【问题描述】:
我有一个集合App.listingList,其中后续的fetch() 用add:true 调用。
App.listingList.fetch({
data: {some:data},
processData: true,
add: true
});
问题:新添加的模型如何在不重新渲染现有模型的视图的情况下渲染其视图。这意味着我不能这样做:
this.collection.each( function(listing, index) {
new ListingMarkerView({ model:listing }).render();
}, this);
尝试 #1
在集合的 add 事件上渲染视图,我无法找到访问要渲染的新模型的方法
ListingListView = Backbone.View.extend({
initialize: function() {
this.collection.bind('add', this.renderNew, this);
},
render: function() {
console.log('render ListingListView');
this.collection.each( function(listing, index) {
new ListingMarkerView({ model:listing }).render();
}, this);
return this;
},
renderNew: function() {
// How do I grab the new models?
new ListingMarkerView({ model:listing }).render(); // wont work
return this;
}
});
尝试 #2
我尝试使用第二个集合来执行后续的fetch,并使用 underscore.js 的_.without() 比较两个集合的模型,但返回的数组仍然包含在作为参数传递的第二个数组中找到的元素.使用_difference() 也返回了与第一个数组相同的数组。
App.listingListNew.fetch({
data: {some:data},
processData: true,
success: function() {
console.log(App.listingListNew.models);
console.log(App.listingList.models);
console.log(_.without(App.listingListNew.models, App.listingList.models));
console.log(_.difference(App.listingListNew.models, App.listingList.models));
}
});
console.log 输出
由于我将 2 个相同的数组传入 _.difference() 和 _.without(),因此输出应该是 []。但它不是:/ 可能是因为cid 不同,所以每一个都被视为唯一?
【问题讨论】:
标签: javascript jquery backbone.js underscore.js