Skip to content

Instantly share code, notes, and snippets.

@koriroys
Forked from vasilakisfil/radio-button-component.js
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koriroys/221de7659a14e5465561 to your computer and use it in GitHub Desktop.
Save koriroys/221de7659a14e5465561 to your computer and use it in GitHub Desktop.
// {{ radio-button name='dish' value='spam' groupValue=selectedDish selectedAction='testAction' }} Spam
// {{ radio-button name='dish' value='eggs' groupValue=selectedDish }} Eggs
//
/*
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'input',
type: 'radio',
attributeBindings: [ 'checked', 'name', 'type', 'value' ],
checked: function () {
if (this.get('value') === this.get('groupValue')) {
Ember.run.once(this, 'takeAction'); //this actually fires 3 times :/
return true;
} else { return false; }
}.observes('groupValue'),
takeAction: function() {
this.sendAction('selectedAction', this.get('value'));
},
change: function () {
this.set('groupValue', this.get('value'));
}
});
*/
import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'input',
type: 'radio',
attributeBindings: [ 'checked', 'name', 'type', 'value' ],
checked: function () {
if (this.get('value') === this.get('groupValue')) {
Ember.run.once(this, 'takeAction');
return true;
} else { return false; }
},
takeAction: function() {
this.sendAction('selectedAction', this.get('value'));
},
change: function () {
this.set('groupValue', this.get('value'));
Ember.run.once(this, 'checked'); //manual observer
}
});
{{ radio-button name='thing' value='one' groupValue=selection selectedAction='testAction' }} One
{{ radio-button name='thing' value='two' groupValue=selection }} Two
Currently selected: {{ selection }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment