Swyd
Swyd

Reputation: 41

What is the best way to presetting checkboxes in Ember.js?

I have template with checkbox

<div class="col-sm-6">
   <input type="checkbox" id="isDeletedCheckbox" {{action "toggleIsDeleted" "isDeletedCheckbox" on="change"}}/>
</div>

And I have Route with

afterModel(model) {
    this.controllerFor(this.get('routeName')).set('model', model)
},

I tried to use

setupController() {
    if (this.controller.get('model').get('isDeleted')) {
        this.controller.set("isDeletedCheckbox", true);
    }
}

or

setupController() {     
   if (this.controller.get('model').get('isDeleted')) {
        $("#isDeletedCheckbox").checked(true);
   }
}

but it does not help to preset the value

Upvotes: 0

Views: 45

Answers (1)

Lux
Lux

Reputation: 18240

You should use the {{input type="checkbox"}} helper.

{{input type="checkbox" name="isEmberized" checked=model.isDeleted}}

If your model looks like this:

model() {
  return { isDeleted: true };
}

What you should never no is something like

$("#isDeletedCheckbox").checked(true);

in a route or controller. Only use direct DOM manipulation in a component!

Upvotes: 2

Related Questions