當在初始fetch
之後調用fetch
(對用戶的操作作出響應)時,許多新抓取的模型可能與初始fetch
。如果我使用add: true
選項呼叫fetch
,則該集合中可能存在重複的模型。Backbone的抓取():僅添加對集合新增的模型
問題:除刪除現有的所有車型從集合(與id=1,2,3,4
說),並插入新取的模型(id=1,2,3,5
),是有可能做到以下2個操作:
添加只有新機型
id=5
,導致與id=1,2,3,4,5
收集。然後僅渲染新視圖(id=5
)添加新模型
id=5
並刪除未在新的fetch
(id=4
)中找到的模型。然後呈現新視圖(id=5
),並刪除舊的意見(id=4
)
嘗試:
與其說App.listingCollection.fetch()
的,使用fetchNew()
功能。這隻適用於將新型號id=5
添加到集合中。
如何在不重新渲染現有視圖id=1,2,3,4
的情況下觸發新視圖的渲染(id=5
)?我內ListingCollection
與線new ListingMarkerView({ model:item }).render();
嘗試這樣做,但我在ListingMarkerView
得到一個錯誤響應線var marker = L.marker([this.model.get('lat'), this.model.get('lng')]);
:
錯誤
Uncaught TypeError: Object #<Object> has no method 'get'
收集
ListingCollection = Backbone.Collection.extend({
model: Listing,
url: '/api/search_by_bounds',
fetchNew: function(options) {
options = options || {};
var collection = this,
success = options.success;
options.success = function(resp, status, xhr) {
_(collection.parse(resp, xhr)).each(function(item) {
// added this conditional block
if (!collection.get(item.id)) {
collection.add(item, {silent:true});
new ListingMarkerView({ model:item }).render();
}
});
if (!options.silent) {
collection.trigger('reset', collection, options);
}
if (success) success(collection, resp);
};
return (this.sync || Backbone.sync).call(this, 'read', this, options);
}
});
查看
ListingMarkerView = Backbone.View.extend({
render: function() {
var marker = L.marker([this.model.get('lat'), this.model.get('lng')]);
markers.addLayer(marker);
},
close: function() {
this.unbind;
}
});
更新:true'做你想要的嗎? ''' App.listingCollection.fetch({update:true}); ''' –