假設我有一個Backbone.Collection
三種型號。我想替換中間的一個,將第一個和第三個模型保留在當前位置。 (假設沒有comparator
。)我該怎麼做?如何替換Backbone.Collection中的模型?
回答
這是今天的一個很好的問題,仍然具有現實意義,所以值得回答。
當你問這個問題時,我的答案中使用的功能是不存在的,但現在這樣做很簡單。
無論如何,假設您的收藏夾名爲myCollection
,並且已經存儲了3個模型。讓我們也說你有一個名爲newModel
的新模型,你想用它來替換集合中現有的中間模型。要更換中間模型,離開第一和第三模式不變,你可以這樣做:
myCollection.remove(myCollection.at(1));
myCollection.add(newModel, {at: 1});
這將不正是你想要的東西,提高remove
事件消失,模型和add
事件的更換,這也是你想要的。
一種方法是直接修改collection.models
數組,然後調用
collection.refresh(collection.models)
然而,這將調出一個add
事件列出每個模型。理想情況下,我想爲已刪除的模型調用remove
事件,併爲新的模型調用add
事件。我不知道這是否是可能的,而無需使用comparator
和我的模型加算指標...
你可以引進骨幹集合中replaceAt功能,將是這個樣子:
replaceAt : function(model, options, replaceIndex) {
this._replaceAt(model, options, replaceIndex);
return this;
},
_replaceAt : function(model, options, replaceIndex) {
options || (options = {});
if (!(model instanceof Backbone.Model)) {
model = new this.model(model, {collection: this});
}
var already = this.getByCid(model);
if (already) throw new Error(["Can't add the same model to a set twice", already.id]);
this._byId[model.id] = model;
this._byCid[model.cid] = model;
model.collection = this;
replacedModel = this.at(replaceIndex)
this.models.splice(replaceIndex, 1, model);
this._cleanModel(replacedModel, options);
model.bind('all', this._boundOnModelEvent);
if (!options.silent) model.trigger('add', model, this, options);
return model;
},
_cleanModel : function (model, options) {
if(!model) return null;
delete this._byId[model.id];
delete this._byCid[model.cid];
delete model.collection;
if(!options.silent) model.trigger('remove', model, this, options);
model.unbind('all', this._boundOnModelEvent);
return model;
},
其基於_add和_remove方法。它應該激發您正在尋找的適當事件,但我沒有爲此進行測試。我測試了它會在適當的位置替換集合中的元素。
(使用骨幹.3.3)
雖然這個問題很久以前就問過了,但即使在今天也是如此。
而且其實很簡單。
- 我們需要獲取集合中的舊模型索引。
- ,然後添加新的到集合在同一索引位置和合並
看看下面的例子:
var index = collection.indexOf(oldModel); //1
collection.add(newModel, {at: index, merge: true}); //2
希望這將有助於有人爲...
好運!
我不相信這是正確的。與'collection.set'一起使用時,'merge:true'接受新模型的屬性,並將它們設置在目標索引處的現有模型上。與'collection.add'一起使用,它不起作用(至少在Backbone 1.1.0中)。 – 2013-12-12 20:29:11
Hi Trevor,我和一位朋友進行了這方面的討論,並指出他在骨幹官方網站中的這個鏈接:[backbone add collection](http://backbonejs.org/#Collection-add)。順便說一句,我嘗試了上面的代碼和它的工作。無論如何謝謝你的反饋。 – 2014-01-12 11:11:10
有內置骨幹一種簡單的機制來處理這個問題
myCollection.add(newModel, {merge: true});
這應該是被接受的答案。 – 2014-10-20 19:39:47
合併和替換是不同的行爲。 OP詢問如何更換。不過,我同意這個答案是資料性的,並且解決了另一個需求。 Upvoted。 – dcarson 2014-12-01 21:55:33
我不同意。 OP詢問了這個答案沒有解決的具體問題。此外,答案並沒有說明它沒有解決OP的原始問題。它可能會混淆其他讀者。對於另一個問題,這將是一個很好的答案。 Downvoted。 – 2015-02-26 13:20:51
- 1. 如何從一個Backbone.Collection刪除模型
- 2. Backbone.Collection通過編號獲取模型
- 3. 添加模型Backbone.Collection不刷新視圖
- 4. 擴展Backbone.Collection和Backbone.Model來創建新的集合/模型類型
- 5. 在Backbone中,一旦模型被破壞,如何替換視圖的模型?
- 6. 替換kinect avateering模型
- 7. 如何替換Makefile.am中的模式
- 8. 實體模型是否替換mvc中的模型
- 9. 用另一個模型值替換模型中的字符串
- 10. Backbone.Model保存 - 返回的模型的子元素不是Backbone.Collection。
- 11. 是否有可能在運行時更改Backbone.Collection的模型
- 12. 從Backbone.Collection的末尾刪除/刪除模型?
- 13. 如何通過有效查看模型來替換域的模型使用
- 14. 如何獲得Backbone.Collection的子集?
- 15. 類型替換的XML模式
- 16. 如何用角色替換用戶模型的路由
- 17. 當你克隆一個Backbone.Collection時,模型引用是否完好?
- 18. 我如何擺脫..替換(替換(替換(替換(替換(...?
- 19. R中的模式替換
- 20. 如何用int替換模板類型T
- 21. 如何替換JavaScript中的「 - 」?
- 22. 替換模型(EF)中的數據庫中的空值
- 23. ng模型可以替換ID嗎?
- 24. 用原始類型替換模板類
- 25. 「模型」對象沒有屬性「替換」
- 26. 如何使用優化方法替換模型陣列中的元素?
- 27. Preg_替換模板中的模式
- 28. 如何替換unix中的模式中的字符
- 29. 在MongoDB中替換類型
- 30. 如何使模型替代CustomMembershipDB
嗯,也許我應該寫一個'LinearCollection'子類,我的目的。 – 2011-06-06 23:24:16