[英]collection.reset not populating with all the models
我的Backbone收藏集這樣調用
collection.fetch({data : {product_type: foo, year: bar}, processData: true, success: function(response){
console.log(response, "response from server");
collection.reset(response);
}});
在Chrome控制台中,它顯示該陣列有六個記錄,但是當我進一步檢查響應時,該陣列中實際上有0個模型。 通過注意第一行的數字6和最后一行的Array [0],您可以在下面看到此信息:
MyCollection {length: 6, models: Array[6], _byId: Object, sortField: "date", sortDirection: "DESC"…}
_byId: Object
_events: Object
_listenId: "l1"
length: 0
model: function (attrs, options) {
models: Array[0]
//...other details ommitted...//
此外,視圖中僅出現6個預期模型中的1個。 鑒於上述情況,我認為可能會出現0或6,但是有1。
為什么集合中的所有6個模型都沒有重置?
基於文檔Collection#fetch我可以看到success
回調具有3個參數
(collection, response, options)
因此,在您的情況下, response
是基於服務器上的JSON
數組更新的collection
,因此您無需再次重置它,而是使用collection重置它意味着您添加了clear集合並添加了實際上是集合本身的新模型。 所以你可以改用
collection.reset(response.models)
但我認為您不應在success
回調中對集合執行任何操作,主干會自動為您執行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.