主干-不能循环通过集合
backbone - cannot loop through a collection
我在一个帮助用数据填充视图的集合中循环时遇到问题。当我试图循环浏览集合并得到以下错误时,
未捕获的类型错误:对象#没有方法"each"
我完全不知道为什么会出现这个错误,除了对象显然没有那个方法之外,但是我只有在运行drop函数时才会出现这个错误——请参阅下面的代码。
这是主干代码
GroupListView.prototype.keyup = function() {
this.filtered = this.collection.searchName(this.$el.find("#search-query").val());
return this.renderList(this.filtered);
};
GroupListView.prototype.renderList = function(collection) {
var $collection, $this;
console.log(this.$el);
console.log(collection);
if (!collection) {
collection = this.collection;
console.log(collection);
}
this.$el.find(".list-items").empty();
$this = this.$el.find("#people-groups-list");
$collection = collection;
if ($collection.length < 1) {
this.$el.find("#people-groups-list").hide();
return this.$el.find("#people-groups-list").after('<div class="activity-no-show">'
<p>To add a new group, click the + in the top right hand corner to get started.</p>'
</div>');
} else {
this.$el.find("#people-groups-list").show();
collection.each(function(item) {
//console.log(item);
var displayView;
displayView = new app.GroupListDisplayView({
model: item,
collection: $collection
});
return $this.append(displayView.render());
});
return this;
}
};
GroupListDisplayView.prototype.render = function() {
var $body;
//console.log(this.model.toJSON());
this.$el.html(this.template({
m: this.model.toJSON()
}));
$body = this.$el.find(".card-body");
$.each(this.model.get("people"), function(i, person) {
var personTile;
this.person = new app.Person({
id: person.id,
avatar: person.avatar,
first_name: person.first_name,
last_name: person.last_name
});
console.log(this.person);
personTile = new app.PersonTileView({
model: this.person
});
return $body.html(personTile.render());
});
return this.$el.attr("id", "group-card-" + this.model.id);
};
GroupListDisplayView.prototype.drop = function(e) {
var $collection, $model, person_id, request;
e.preventDefault();
$collection = this.collection;
person_id = e.originalEvent.dataTransfer.getData('Text');
request = new app.PersonGroupAdd;
$model = this.model;
return request.save({
async: true,
wait: true,
person_id: person_id,
group_id: this.model.get("id")
}, {
success: function(d) {
return $model.fetch({
async: true,
wait: true
});
}
});
};
GroupListView.prototype.initialize = function() {
this.collection.on("change", this.renderList, this);
this.collection.on("reset", this.render, this);
return this.renderList();
};
试试这个,把这个函数放在你的集合中
parse: function (data) {
data.forEach(function (item) {
displayView = new app.GroupListDisplayView({
model: item,
collection: $collection
});
});
}
希望这能有所帮助。
我不确定drop
函数与它有什么关系,但我看到renderList
在keyup
期间传递了searchName
的结果。可能发生的情况是,searchName
返回一个正则数组,而不是一个具有each
方法的包装对象(即Backbone.Collection、jQuery集合或Underscore包装对象)。
不要调用collection.each
,而是使用jQuery或Undercore:
$.each(collection, function(i, item) { ... });
或
_.each(collection, function(item, i, list) { ... });
相关文章:
- JavaScript:for循环中的集合为null
- BackboneJs如何在集合中循环,从每个模型中获取特定值
- 循环访问集合中的数组
- 在循环访问集合时将动态 ID 分配给隐藏字段
- 循环(将项目存储在集合中)似乎迭代的次数比它应该的次数少
- 主干并循环访问集合,但结果未追加到 $(this.el)
- 使用下划线循环遍历集合
- HTML集合-While循环期间删除元素
- 使用嵌套的“”循环迭代流星中的集合;forEach”;
- 流星:在集合内的对象上循环
- for循环为什么以及何时忽略具有html集合的某些项
- 循环数组并在循环中检查MongoDB集合(Async)
- 如何拥有一个可观察的集合,该集合可以从两个源进行更新,而不会在knockout.js中引起循环
- 循环使用jQuery集合并使用jQuery进行包装
- 主干-不能循环通过集合
- 我如何循环通过json集合的字符串与胡子
- 循环遍历一个集合并返回一个新集合,其中组合了具有相同id的对象
- 循环遍历Firebase数据库记录的集合
- 主干循环遍历并从嵌套集合中获取值
- 当我通过循环运行返回的元素集合时,JavaScript正在复制文档get方法返回的元素