【问题标题】:How to set multiple attributes in model (Backbone.js) using strings as keys?如何使用字符串作为键在模型(Backbone.js)中设置多个属性?
【发布时间】:2015-01-26 03:09:11
【问题描述】:

我有一个类似的问题:setting backbone models,但我需要设置多个属性,而不仅仅是一个。

所以,我有一个模型,有人可以在其中添加自定义属性,但我在设置它们时遇到了问题,因为如果我这样做:

parameter.set({
    name: nameValue, 
    value: valueValue,
    description: descriptionValue
});

其中name, valuedescription 是字符串值,主干正在创建一个名为“名称”、“值”和“描述”的新属性,但我实际上需要它们是“名称”的值等等

如果我尝试这样做:

parameter.set(name, nameValue);
parameter.set(value, valueValue);
parameter.set(description, descriptionValue);

backbone 只创建最后一行:parameter.set(description, descriptionValue);

当我尝试这样做时,也会发生同样的事情:

parameter.attributes[name] = nameValue;
parameter.attributes[value] = valueValue;
parameter.attributes[description] = descriptionValue;               

例如,假设我有这个 HTML 代码:

<input type="text" id="name"/>
<input type="text" id="nameValue"/>

<input type="text" id="value"/>
<input type="text" id="valueValue"/>

<input type="text" id="description"/>
<input type="text" id="descriptionValue"/>

地点:

var name = this.$('#name');
var nameValue = this.$('#nameValue');
var value = this.$('#value');
var valueValue = this.$('#valueValue');
var description = this.$('#description');
var descriptionValue = this.$('#descriptionValue');

【问题讨论】:

  • 那么您要创建一个具有valuedescription 属性的name 吗?
  • 不,我想把用户在文本框中输入的参数属性放在里面(我更新了代码)
  • 当您尝试更新模型时,您确定拥有name valuedescription 的值(它们不为空或未定义)吗?
  • 这肯定不是骨干问题,而是一些其他代码,你能创建一个小提琴来演示这个问题

标签: backbone.js


【解决方案1】:

您可以像这样添加它们:

var name = this.$('#name').val();
var nameValue = this.$('#nameValue').val();
var value = this.$('#value').val();
var valueValue = this.$('#valueValue').val();
var description = this.$('#description').val();
var descriptionValue = this.$('#descriptionValue').val();

var params = {};

if (name) {
    params[name] = nameValue;
}
// the same for other attributes
// ....
// And at the end

model.set(params);

If 语句将确保属性名称存在。然后将它们收集到一个空对象中。最后一次设置它们。

更新

您可以在下面找到working 您的问题示例。填写输入,然后单击提交以对您要构造的对象进行字符串化。

var MyForm = Backbone.View.extend({
    el: '#test', 
    events: {
        "click .submit": "showObject" 
    },
    showObject: function(e) {
      e.preventDefault();
      var name = this.$('#name').val();
      var nameValue = this.$('#nameValue').val();
      var value = this.$('#value').val();
      var valueValue = this.$('#valueValue').val();
      var description = this.$('#description').val();
      var descriptionValue = this.$('#descriptionValue').val();
      
      var params = {};

      if (name) {
          params[name] = nameValue;
      }
      
      if (value) {
          params[value] = valueValue;
      }
      
      if (description) {
          params[description] = descriptionValue;
      }
      
      
      this.model.set(params);
      
      
      this.$('.test-object').text(JSON.stringify(this.model.toJSON()));
    }});

var TestModel = Backbone.Model.extend({});

var myForm = new MyForm({model: (new TestModel()) });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone.js"></script>

<div id="test">
  <input type="text" id="name"/>
  <input type="text" id="nameValue"/>

  <input type="text" id="value"/>
  <input type="text" id="valueValue"/>

  <input type="text" id="description"/>
  <input type="text" id="descriptionValue"/>
  <div class="test-object"></div>
  <button class="submit">Show object</button>
</div>

【讨论】:

  • 其实,不,这是不对的。此解决方案为您提供与我已经提到的解决方案相同的结果。
  • @hlamija 我已经更新了答案,请检查工作代码 sn-p。
  • 对我来说它不起作用:(它仍然只添加了params中的最后一项,而不是全部
  • @hlamija 正如您从代码 sn-p 中看到的那样,它正在工作。我猜您的代码中还有另一个问题。您可以在将每个设置的参数设置到模型之前对其进行控制台记录吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-26
  • 1970-01-01
  • 2013-04-14
  • 2014-02-08
相关资源
最近更新 更多