Skip to content

Instantly share code, notes, and snippets.

@ashrafhasson
Last active August 29, 2015 14:05
Show Gist options
  • Save ashrafhasson/9d27b6b0bafd7e070888 to your computer and use it in GitHub Desktop.
Save ashrafhasson/9d27b6b0bafd7e070888 to your computer and use it in GitHub Desktop.
ember-radio-button-group
<label class="label">{{name}}</label>
{{#each option in options}}
{{radio-button checked=option.checked value=option.value title=option.title name=name class='toggle'}}
{{/each}}
import Ember from 'ember';
export default Ember.Component.extend({
name: Ember.required(),
selected: null
});
<input type="radio" {{bind-attr checked=checked value=value name=name}}>
<i class="rounded-4x"></i> {{title}}
import Ember from 'ember';
export default Ember.Component.extend({
attributeBindings: ['class'],
tagName: 'label',
checked: false,
change: function() {
// console.log('we detected a change to: ' + this.$('input').prop('value'));
var isChecked = this.$('input').prop('checked');
this.set('checked', isChecked);
if (isChecked) {
this.set('parentView.selected', this.$('input').prop('value'));
}
},
otherChanged: function() {
var selected = this.get('parentView.selected');
var isChecked = this.get('checked');
var value = this.$('input').prop('value');
var isCheckedNew = (!Ember.isEmpty(selected) && value === selected);
if (isChecked !== isCheckedNew) {
this.set('checked', isCheckedNew);
// console.log('changed our checked property in ' + value + ' to: ' + this.get('checked'));
}
}.observes('parentView.selected')
});
{{!--
Usage in handlebars template
To generate a group of radio buttons that are mutually exclusive,
simply bind to the `options` attribute of the radio-button-group
and set the `name` attribute to what you'd like the radio button
group input elements be named.
The `options` property should be bound to a property on your
controller that returns an array of objects. See the controller
example that complements this test template example.
To set the radio-button's class, define the `toggle` class in your
css file.
--}}
{{radio-button-group optionsBinding="items" name='Items'}}
Selected: {{selected}}
// test controller
// To detect change in the selection, you can watch the items' `checked`
// property.
import Ember from 'ember';
export default Ember.ArrayController.extend(
items: function () {
return [
Ember.Object.create({value: 'any', title: 'Any', checked: true}),
Ember.Object.create({value: 'manual', title: 'Manual', checked: false}),
Ember.Object.create({value: 'automatic', title: 'Automatic', checked: false})
];
}.property(),
selected: function () {
return this.get('items').findBy('checked').get('title');
}.property('items.@each.checked')
);
@dotcomputercraft
Copy link

Do you have a running example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment