Skip to content

Instantly share code, notes, and snippets.

@courajs
Created June 3, 2014 17:01
Show Gist options
  • Save courajs/84f5b113de2db762cb1d to your computer and use it in GitHub Desktop.
Save courajs/84f5b113de2db762cb1d to your computer and use it in GitHub Desktop.
Cooler magic binding radio buttons
import RadioView from '../views/radio';
export default Ember.Handlebars.makeViewHelper(RadioView);
<label>
{{radio-button checked='thing' value='one'}} One
</label>
<br>
<label>
{{radio-button checked='thing' value='two'}} Two
</label>
<br>
<div>Currently selected: {{thing}}</div>
var RadioView = Ember.View.extend({
tagName: 'input',
type: 'radio',
attributeBindings: ['type', 'htmlChecked:checked', 'value', 'checked:name'],
change: function(){
this.set('selectedValue', this.get('value'));
},
htmlChecked: function(){
return this.get('value') === this.get('selectedValue');
}.property('value', 'selectedValue'),
setupBindings: function() {
if (this.binding) this.binding.disconnect(this);
this.binding = Em.Binding.from("context." + this.get('checked')).to('selectedValue');
this.binding.connect(this);
}.on('init').observes('checked', 'context')
});
export default RadioView;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment