2012-08-28 80 views

回答

1

簡單環路有什麼問題?

m.set('selected', false) for m in @model.collection.where(selected: true) 

甚至:

for m in @model.collection.where(selected: true) 
    m.set('selected', false) 

下劃線是好的,但是,這並不意味着你必須使用它的一切。

1

您可以.each

_.each(this.model.collection.where({selected: true}), function(m){ 
    m.set('selected', false); 
}); 

做,因爲where returns an array of matching objects,你必須該數組傳遞到了下劃線的each的第一個參數。

你也可以用map做到這一點:

this.model.collection.map(function(m){if(m.get('selected'){m.set('selected', false);}}); 

由於map採取的每一個元素的集合(或陣列)中,適用的方法給他們。

+0

我真的很喜歡這些解決方案,我用@ mu_is_too_short的答案去了,因爲它看起來更像coffeescript。感謝您抽出時間幫忙! –

-1
this.model.collection.where({selected: true}).each(function(model){ 
    model.set({selected:false}); 
}