Skip to content

Instantly share code, notes, and snippets.

@cainlevy
Last active March 3, 2016 18:27
Show Gist options
  • Save cainlevy/cd092bbe7cd07142615e to your computer and use it in GitHub Desktop.
Save cainlevy/cd092bbe7cd07142615e to your computer and use it in GitHub Desktop.
Checkbox Desync w/ Group Select

REPRO

  1. click Group
  2. DESYNC: notice that Group is unchecked and true

VARIATION: on change

  1. modify template to bind the action on="change"
  2. click Foo
  3. notice that Group is checked and true
  4. click Group
  5. DESYNC: notice that Group is unchecked and true

SOLUTION: runloop

bind to the default action and wrap the inside of toggleGroup inside Ember.run.next.

import Ember from 'ember';
export default Ember.Controller.extend({
foo: false,
bar: false,
fooOrBar: Ember.computed.or('foo', 'bar'),
fooAndBar: Ember.computed.and('foo', 'bar'),
actions: {
toggleGroup: function () {
if (this.get('fooAndBar')) {
this.set('foo', false);
this.set('bar', false);
} else {
this.set('foo', true);
this.set('bar', true);
}
}
}
});
<fieldset>
<legend>
<input type="checkbox" checked={{fooOrBar}} {{action 'toggleGroup' on='change'}} />
Group ({{fooOrBar}})
</legend>
<div>
{{input type="checkbox" checked=foo}}
Foo
</div>
<div>
{{input type="checkbox" checked=bar}}
Bar
</div>
</fieldset>
{
"version": "0.6.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.3.1/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.3.3/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment