Search code examples
javascriptember.jscheckboxhandlebars.js

Checkboxes in Ember Js


I have a data fixture adapter say..

App.Person.reopenClass({
FIXTURES: [
    {
        id: 1,
        name: 'Name1',
    },
    {
        id:2,
        name:'Name2'
    }
]

});

In my template i want to bind this model with checkboxes..like there are two names in the model so there should be two checkboxes with name as their labels

This is my route

App.IndexRoute=Ember.Route.extend({
model:function(){
    return this.store.findAll('person');
}

});

and this is Controller-on click of a button I want to retrieve info about the checkboxes

App.IndexController=Ember.ArrayController.extend({

actions:{
    buttonHandler:function(){
             //Get Names which are checked/unchecked
        }}
});

Is there any way to bind the model with checkboxes and retrieve which checkboxes have been selected in the controller?


Solution

  • App.Person.reopenClass({
     FIXTURES: [
     {
        id: 1,
        name: 'Name1',
        checked: false
     },
     {
        id:2,
        name:'Name2',
        checked: false
     }
    ]);
    

    your template:

    {{#each controller.model as |obj|}}
      {{input type="checkbox" name=obj.name checked=obj.checked}}
    {{/each}}
    <button {{action 'buttonHandler'}}>Get checked</button>
    

    your controller:

    App.IndexController=Ember.ArrayController.extend({
    
     actions:{
       buttonHandler:function(){
          var checked_only = this.get('model').filterBy('checked', true);
       }}
    });
    

    Hope this helps...