简体   繁体   中英

Bind an Ember checkbox to the opposite value of a property

I have a model foo with the property bar . When I render an ember checkbox, I want to bind the checked property to foo.bar such that when the box is checked foo.bar is false and when unchecked, foo.bar is true

Doing:

{{input
  type='checkbox'
  checked=foo.bar}}

gets me the opposite of what I want, while

{{input
  type='checkbox'
  checked=(not foo.bar)}}

does not bind the value of foo.bar to the checked property. How can I solve this without adding a new notBar property to foo ?

You can do this:

<input
 type='checkbox'
 onchange={{action (mut foo.bar) (not foo.bar)}}
 checked={{not foo.bar}}>

You dont need an external toggle helper for this.

This does the trick:

<input
 type='checkbox'
 onchange={{action (toggle 'bar' foo)}}
 checked={{not foo.bar}}>

Using https://github.com/DockYard/ember-composable-helpers#toggle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM