我将尝试在{{#each}}
块中使用给定的JSON数组填充多个单选按钮。
我的单选按钮视图:
Ember.View.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()
});
模板:
{{#each shippingOptions}}
{{view "radiobutton" id=id name="shippingCosts" selectionBinding="choosenShippingCosts" value=id}}
<label {{bind-attr for=id}}>{{label}}</label>
{{/each}}
控制器:
choosenShippingCosts: 'tomorrow',
shippingOptions JSON数组:
[
{
"id":"tomorrow",
"value":1,
"label":"wednesday 16 april shipped on market day (€1)"
},
{
"id":"marketPickup",
"value":0,
"label":"wednesday 16 april picked up on market (free)"
},
{
"id":"firstFriday",
"value":3.95,
"label":"friday 11 april shipping (€3.95)"
}
]