ember.js - 在 Ember.js 中处理多个复选框数组

标签 ember.js

这是我有一个表单,其中有多个复选框,并且它们具有相同的名称属性“classifications[]”

使用代码:

<input type="checkbox" name="classification[]" value="Value 1" />
<input type="checkbox" name="classification[]" value="Value 2" />
<input type="checkbox" name="classification[]" value="Value 3" />

默认情况下它可以正常工作,它会像这样发布“分类[]”

[ 0 => "Value 1", 1 => "Value 2"]

但我希望它能在 ember 应用程序中工作,所以我这样做了(缩短版本)

// index.html
{{input type="checkbox" name="classification[]" value="Cell Member" checked=classification}}

App.ApplicationController = Ember.Controller.extend({
  var user = this.store.createRecord('user',{
  classification: this.get("classification")
})
  user.save();
})

App.User = DS.Model.extend({
 classification: DS.attr("string")
});

但唯一发布的分类值是 True..

最佳答案

试试这个

// index.html
{{#for possibleValues}}
    <label>{{this.label}}</label>
    {{input type="checkbox" value=this.isChecked}}
{{/for}}
<a {{action save}}>save</a>

var possibleValue = [{
    label: 'Label for value 1',
    isChecked: false,
    value: 'Value 1'
  },{
    label: 'Label for value 2',
    isChecked: false,
    value: 'Value 2'
  },{
    label: 'Label for value 3',
    isChecked: false,
    value: 'Value 3'
  }];

App = Ember.Application.create();
App.ApplicationAdapter = DS.RESTAdapter.extend();
App.ApplicationController = Ember.ObjectController.extend({
  init: function () {
    this._super();
    var user = this.store.createRecord('user');
    this.set('content', user);
  },
  actions:{
    save: function () {
      var user = this.get('content'),
          collected = user.get('classifications');
      this.set('collected', collected);
      user.save();

     }
  },
  //executes when isChecked is changed in one item of possibleValues
  collectChecked: function () {
    var classifications;
    classifications = this.get('possibleValues').filterBy('isChecked', true); //filter checked only 
    classifications = classifications.mapBy('value'); //map values
    this.set('classifications', classifications);
    console.log(classifications);
  }.observes('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aadac5d9d9c3c8c6cffccbc6dfcfd984eacfcbc9c284c3d9e9c2cfc9c1cfce" rel="noreferrer noopener nofollow">[email protected]</a>'),
  possibleValues: possibleValues
});

App.RawTransform = DS.Transform.extend({
  deserialize: function(serialized) {
    return serialized;
  },
  serialize: function(deserialized) {
    return deserialized;
  }
});

App.User = DS.Model.extend({
  classifications: DS.attr('raw')
});

http://jsbin.com/dokiwati/6/edit

关于ember.js - 在 Ember.js 中处理多个复选框数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22990412/

相关文章:

javascript - emberjs 没有处理该事件

javascript - 安贝尔吉斯 (Emberjs) 景观

javascript - 如何使用 ember-model 定义枚举

ember.js - 如何在验收测试中模拟 Ember-CLI 服务?

Ember.js:使用 {{action}} 时,如何定位特定 Controller ?

javascript - 动态子属性更改时如何更新 Ember 计算属性

javascript - 按 ember.js objectController 中的属性排序

ember.js - 从索引 Controller 发送的 Ember 操作未由索引路由处理

css - 如何在 Ember 中使用 css 动画

ember.js - 使用 Handlebars 定义 block 助手