【发布时间】:2013-04-15 12:12:12
【问题描述】:
我确信这是一个非常容易解决的问题,但到目前为止我发现的所有帖子似乎都没有直接解决这个问题:我如何遍历集合以获取每个模型?
我尝试使用的第一种方法是下划线的 each 方法。这是我的电话和功能:
collection_var.each(paintThings);
这是我的功能:
function paintThings() {
console.log(this);
console.log(this.model);
var thing_type = this.model.get("type"),
thing_other = this.model.get("otherAttribute");
console.log(this.model);
console.log(thing_type);
console.log(thing_other);
}
现在,这是未定义的,并且 this.model 错误:
Uncaught TypeError: Cannot read property 'model' of undefined
我知道答案很简单,但它让我发疯!我是下划线的新手。这里有人可以帮忙吗?如果它们更快/更好,我也对其他非下划线方法持开放态度。
我也试过这个:
for (var i = 0, l = collection_var.length; i < l; i++) {
console.log(collection_var[i]);
}
但这也不是我想要的。
【问题讨论】:
标签: javascript backbone.js underscore.js