Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a component that generates Radio-buttons in Ember.js?

Can I and should i pass arrays to components in ember?

For example if I wanted to template something that renders a couple of radiobuttons with labels:

<label for="media">Media</label><input type="radio" name="entry.1602323871" value="media" id="media" />
<label for="guest">Guest</label><input type="radio" name="entry.1602323871" value="guest" id="guest" />

Could I somehow pass an array with this content and loop through it.

Media, media
Guest, guest
like image 427
Himmators Avatar asked Nov 28 '13 14:11

Himmators


1 Answers

Yeah, You can pass anything to components. Just a try to the radio-buttons

//Basic radio Component
App.RadioButton = Ember.Component.extend({
  tagName : "input",
  type : "radio",
  attributeBindings : [ "name", "type", "value", "checked:checked" ],
  click : function() {
    this.set("selection", this.$().val());
  },
  checked : function() {
    return this.get("value") === this.get("selection");
  }.property('selection')
});
Em.Handlebars.helper('radio-button',App.RadioButton);

Updated (component name should be dasherized)

Working Radio Demo

like image 107
selvagsz Avatar answered Oct 11 '22 16:10

selvagsz