admin管理员组文章数量:1026989
Why is the item variable undefined in this Backbone example?
var Action = Backbone.Model.extend({
defaults: {
"selected": false,
"name": "First Action",
"targetDate": "10-04-2014"
}
});
var Actions = Backbone.Collection.extend({
model: Action
});
var actionCollection = new Actions( [new Action(), new Action(), new Action(), new Action()]);
_.each(actionCollection, function(item) {
alert(item);
});
jsFiddle here: /
Why is the item variable undefined in this Backbone example?
var Action = Backbone.Model.extend({
defaults: {
"selected": false,
"name": "First Action",
"targetDate": "10-04-2014"
}
});
var Actions = Backbone.Collection.extend({
model: Action
});
var actionCollection = new Actions( [new Action(), new Action(), new Action(), new Action()]);
_.each(actionCollection, function(item) {
alert(item);
});
jsFiddle here: http://jsfiddle/netroworx/KLYL9/
Share Improve this question asked Jul 1, 2013 at 9:29 Greg Pagendam-TurnerGreg Pagendam-Turner 2,5525 gold badges35 silver badges52 bronze badges2 Answers
Reset to default 10Change it to:
actionCollection.each(function(item) {
alert(item);
});
And it works fine.
This because actionCollection is not an array, so _.each(collection) does not work but collection.each does because that function is build into Backbone collection.
That being said, this also works:
_.each(actionCollection.toJSON(), function(item) {
alert(item);
});
Because now the collection is an actual array.
_.each
accepts an array as first argument, but you passed a Collection
.
Just use the Collection.each
method:
actionCollection.each(function(item){
//do stuff with item
});
Why is the item variable undefined in this Backbone example?
var Action = Backbone.Model.extend({
defaults: {
"selected": false,
"name": "First Action",
"targetDate": "10-04-2014"
}
});
var Actions = Backbone.Collection.extend({
model: Action
});
var actionCollection = new Actions( [new Action(), new Action(), new Action(), new Action()]);
_.each(actionCollection, function(item) {
alert(item);
});
jsFiddle here: /
Why is the item variable undefined in this Backbone example?
var Action = Backbone.Model.extend({
defaults: {
"selected": false,
"name": "First Action",
"targetDate": "10-04-2014"
}
});
var Actions = Backbone.Collection.extend({
model: Action
});
var actionCollection = new Actions( [new Action(), new Action(), new Action(), new Action()]);
_.each(actionCollection, function(item) {
alert(item);
});
jsFiddle here: http://jsfiddle/netroworx/KLYL9/
Share Improve this question asked Jul 1, 2013 at 9:29 Greg Pagendam-TurnerGreg Pagendam-Turner 2,5525 gold badges35 silver badges52 bronze badges2 Answers
Reset to default 10Change it to:
actionCollection.each(function(item) {
alert(item);
});
And it works fine.
This because actionCollection is not an array, so _.each(collection) does not work but collection.each does because that function is build into Backbone collection.
That being said, this also works:
_.each(actionCollection.toJSON(), function(item) {
alert(item);
});
Because now the collection is an actual array.
_.each
accepts an array as first argument, but you passed a Collection
.
Just use the Collection.each
method:
actionCollection.each(function(item){
//do stuff with item
});
本文标签: javascriptBackbone each undefinedStack Overflow
版权声明:本文标题:javascript - Backbone each undefined - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745655966a2161598.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论