Sean Mackesey
Sean Mackesey

Reputation: 10939

Bind Checkbox Settings to Array Content in EmberJS

I have a group of checkboxes in an EmberJS app. I would like to maintain a property in the controller that corresponds to the checked boxes (e.g. contains a string entry with the id of each box that is checked). This property should update itself as boxes are checked or unchecked. What is the best way to do this?

Upvotes: 0

Views: 115

Answers (1)

Yury Svechinsky
Yury Svechinsky

Reputation: 336

Computed property would do

F.e.

App.MyController = Em.ObjectController.extend({

  checkboxValues : [Em.Object.create({id:1, check:false}), Em.Object.create({id:2, check:true})],

  checkedIds : function() {
    return this.get('checkboxValues').filterBy('check').mapBy('id').join(',');
  }.property('[email protected]')

});

//template

{{#each checkboxValues}}
  {{id}} {{input type="checkbox" name="checkbox" checkedBinding="check"}}
{{/each}}

{{checkedIds}}

Upvotes: 1

Related Questions