当在初始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}); ''' –