2012-12-07 15 views
1

我有填充了以下型号的集合:批量更新手动设置的模型

rows: [ 
id: 1 
category: 'Meetings & Banquets' 
progress: 0.8 
, 
id: 2 
category: 'Guest Services' 
    progress: '50%' 
, 
id: 3 
category: 'Concessionaires' 
progress: '20%'  
, 
id: 4 
category: 'Telecommunications' 
progress: 100 
, 
id: 5 
category: 'Restaurant' 
progress: '70%'              
] 

我想批量它们的更新两个像这样和具有被绑定到视图更新模型自己:

rows: [ 
id: 1 
category: 'Meetings & Banquets' 
progress: 0.9 
, 
id: 2 
category: 'Guest Services' 
    progress: '10%' 
, 
] 

当我做。新增或.reset段就可以阻止我添加/更新具有相同id模型中的集合。我有哪些选项批量更新这些模型?

回答

2

编辑:与骨干0.9.9您现在可以使用add方法与选项{merge: true}

addcollection.add(models, [options])

如果要添加的模型有已收集在收集,他们会被忽略,除非你通过{merge:true},在这种情况下,他们的属性将被合并到相应的模型中,触发任何适当的“更改”事件。


我不相信有是,它会为你现在在骨干集合的功能。也许你可以创建自己的?

MyCollection = Backbone.Collection.extend({ 
    model: MyItem, 

    update: function(updatedArray) { 
    var that = this; 
    _.each(updateArray, function(element) { 
     if(that.get(element.id)) { 
     that.get(element.id).set(element); 
     } 
    }); 
    } 
}); 

则:

var collection = new MyCollection(rows); //creates your collection 
collection.update(rows2); //updates specific models. 
+1

这实际上是对骨干GitHub上的问题,他们已经在主分支解决它,请参阅https://github.com/documentcloud/backbone/pull/1752 – imrane

+0

似乎他们仍在讨论https://github.com/documentcloud/backbone/pull/1873会很酷,如果他们确实有所有这些不同的更新选项。 – hajpoj