【发布时间】:2012-08-29 06:02:58
【问题描述】:
this.model.collection.where({selected: true}) 返回一个模型数组
然后我想将返回模型的selected 属性设置为false。
我该怎么做?
@model.collection.where({selected: true})(咖啡脚本)
【问题讨论】:
标签: javascript backbone.js coffeescript underscore.js
this.model.collection.where({selected: true}) 返回一个模型数组
然后我想将返回模型的selected 属性设置为false。
我该怎么做?
@model.collection.where({selected: true})(咖啡脚本)
【问题讨论】:
标签: javascript backbone.js coffeescript underscore.js
简单的循环有什么问题?
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});
}
【讨论】: