this.model.collection.where({selected: true})
返回的車型篩選模型,然後設置屬性對於那些模型
我想然後設置返回模型的selected
屬性設置爲false的數組。
我該怎麼做?
@model.collection.where({selected: true})
(CoffeeScript的)
this.model.collection.where({selected: true})
返回的車型篩選模型,然後設置屬性對於那些模型
我想然後設置返回模型的selected
屬性設置爲false的數組。
我該怎麼做?
@model.collection.where({selected: true})
(CoffeeScript的)
簡單環路有什麼問題?
m.set('selected', false) for m in @model.collection.where(selected: true)
甚至:
for m in @model.collection.where(selected: true)
m.set('selected', false)
下劃線是好的,但是,這並不意味着你必須使用它的一切。
您可以.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
採取的每一個元素的集合(或陣列)中,適用的方法給他們。
this.model.collection.where({selected: true}).each(function(model){
model.set({selected:false});
}
我真的很喜歡這些解決方案,我用@ mu_is_too_short的答案去了,因爲它看起來更像coffeescript。感謝您抽出時間幫忙! –