简体   繁体   English

如何更新集合中的所有模型-Backbone.js

[英]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.

相关问题 Backbone.js-如何将3个模型合并为1个集合并输出所有数据? - Backbone.js - How to combine 3 models into 1 collection and output all the data? 如何使用Backbone.js从服务器获取模型集合? - How can I get a collection of models from the server using Backbone.js? 当其中一个模型被更改时,如何为Backbone.js中的集合视图附加事件处理程序? - How can I attach event handler for a collection view in Backbone.js when one of the models is changed? backbone.js将模型添加到集合中会添加到集合中的所有模型 - backbone.js adding model to collection adds to all models in collection 使用backbone.js获取集合(所有模型)的总和 - Getting the sum of a collection (all models) with backbone.js Backbone.js-如何从集合中获取值? - Backbone.js - How I can get values from collection? 如何在 backbone.js 中保存一个集合或至少一部分 - How can I save a collection, or atleast a portion of it, in backbone.js 如何在本地存储中存储包括所有模型在内的“集合”? (Backbone.js的) - How to store a “collection” including all models in the local-storage? (backbone.js) Backbone.js更新集合中的模型 - Backbone.js updating of models in a collection JavaScript:Backbone.js填充模型集合 - JavaScript: Backbone.js populate collection of models
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM