[英]How can I update all models in a collection - Backbone.js
I am setting the value
attribute of all models in a collection (in an _.each
loop). 我正在设置集合中所有模型的
value
属性(在_.each
循环中)。 After the loop is finished, each value
attribute is unchanged. 循环完成后,每个
value
属性不变。 I believe this is because the _.each
loop is creating a new instance variable for each model. 我相信这是因为
_.each
循环正在为每个模型创建一个新的实例变量。 Is there a standard way to update models of a collection that I am not following? 有没有更新我不关注的集合模型的标准方法?
The code which should force the dice into a straight (1,2,3,4,5,6). 该代码应将骰子强制成直线(1,2,3,4,5,6)。
rollableDice
refers to a Backbone collection of Backbone models which have a value
attribute. rollableDice
是指具有value
属性的Backbone模型的Backbone集合。 This is a method on a container model: 这是容器模型上的一种方法:
makeStraight: function() {
console.log('making straight');
console.log(_(this.rollableDice.models).pluck('value'));
var counter = 1;
_(this.rollableDice.models).each(function(die) {
console.log(die.get('value'));
die.set('value', counter);
console.log(die.get('value'));
counter++;
});
console.log(_(this.rollableDice.models).pluck('value'));
},
This is the output that I see on the console: 这是我在控制台上看到的输出:
making straight
[1, 1, 3, 5, 2, 1]
undefined
1
undefined
2
undefined
3
undefined
4
undefined
5
undefined
6
[1, 1, 3, 5, 2, 1]
//EDIT This is the console output that I expect to see: //编辑这是我期望看到的控制台输出:
making straight
[1, 1, 3, 5, 2, 1]
1
1
1
2
3
3
5
4
2
5
1
6
[1, 2, 3, 4, 5, 6]
//EDIT I'm going to show the model code in response to @numbers1311407's response. //编辑我将显示模型代码以响应@ numbers1311407的响应。 I should not be using a model method to store and access attributes.
我不应该使用模型方法来存储和访问属性。 Instead, I should use the get/set methods or defaults method ( http://backbonejs.org/#Model-defaults ) even while inside the model.
相反,即使在模型内部,我也应该使用get / set方法或defaults方法( http://backbonejs.org/#Model-defaults )。 This is what not to do :
这是不可以做的事情 :
var Die = Backbone.Model.extend({
initialize: function() {
_.bindAll(this, 'roll')
},
value: 1,
roll: function() {
this.value = _.random(1,6);
},
});
Simply use the collection.each
method. 只需使用
collection.each
方法。
myCollection.each(function(model) {
// do stuff
});
Checkout Underscore.js method extend by Backbone.Collection: http://backbonejs.org/#Collection-Underscore-Methods 结帐Underscore.js方法由Backbone.Collection扩展: http : //backbonejs.org/#Collection-Underscore-Methods
You're already updating all the models in the collection, but you're deceived into thinking you aren't by accessing the value incorrectly when logging. 您已经在更新集合中的所有模型,但是您被误以为您不是在登录时错误地访问了该值。
console.log(_(this.rollableDice.models).pluck('value'));
This line does not get
the value of the model, it's logging a value
property on the model instance itself, suggesting that you initialized the models improperly. 该行未
get
模型的值,而是在模型实例本身上记录了value
属性,表明您未正确初始化模型。
pluck
is a collection method (as is each
, as @SimonBoudrias points out). pluck
是一个集合的方法(如each
,作为@SimonBoudrias指出)。 You should use the designated methods when working with collections where possible, rather than wrapping them with underscore yourself. 在可能的情况下使用集合时,应使用指定的方法,而不要用下划线将它们包装起来。
Using Collection#pluck
, your first and last console logs would look like this: 使用
Collection#pluck
,您的第一个和最后一个控制台日志将如下所示:
console.log(this.rollableDice.pluck("value"));
Using that, you'll see something closer to what you expect, but as pointed out, the initial log will still be an array of undefined
, as value
appears to be a property of the model instance, not a proper Model attribute. 使用它,您将看到更接近您期望的内容,但是如前所述,初始日志仍然是
undefined
数组,因为value
似乎是模型实例的属性,而不是适当的Model属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.