在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" />

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

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

但是我想让它在余烬应用程序中工作,所以我做了这个(简化版)

// 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。

g13013

试试这个

// 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('[email protected]'),
  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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章