3

Is there a way to bind the property of a checkbox to a property in the model, for example:

I have this simple model on a route:

export default Ember.Route.extend({
   model:function(params) {
       return { "isAdmin": true }
   } 
});

And on the template I want to display the checkbox checked when the isAdmin is true:

 <input id="adminControll" name="isAdmin" type="checkbox" onchange={{action "toggleAdmin" model.isAdmin}} />

Any ideas on how to achieve this?

2 Answers 2

3

Since Ember 1.13.3 you can do the following:

<input type="checkbox" checked={{model.isAdmin}} 
        onchange={{action "toggleAdmin" value="target.checked"}}>

controller (or component):

actions: {
   toggleAdmin(value) {
      this.set('model.isAdmin', value);
     // Do something useful
   }
}

Here is Ember Twiddle

Sign up to request clarification or add additional context in comments.

Comments

0

You can use {{input}} helper,

{{input type="checkbox" name="isEmberized" checked=isEmberized}}

And bind the checked property to model.isAdmin

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

A working sample is here (via ember-twiddle.com).

You can read more about how to use the input (checkbox in this case) Ember helper at link below,

http://emberjs.com/api/classes/Ember.Templates.helpers.html#toc_checkbox

If you need to handle actions read here,

https://guides.emberjs.com/v2.11.0/templates/input-helpers/#toc_actions

If you really do NOT want to user the input helper, set the value attribute for the input HTML element instead of passing it to the action.

Let me know if above info. solve your problem.

9 Comments

this works, but now my action does not. Is there a way to trigger the action when the checked changes?
What action you need to handle? And what does it used for?
when the checked value changes I need to recalculate some values, if my user is an admin I query an ajax service to get the current rate of discount.
Can you just make the recalculate value to another computed property?
I don't think so, because my model is a custom model created from many data sources. Basically my model is a return Ember.$.ajax().then(function () { return Ember.$.ajax()})
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.