I have a handlebars template in an ember application. It accepts an array. I currently declare the array like this
template:
{{Gd-radio-input content=radioContent value="blue"}}
Javascript:
App.IndexController = Em.Controller.extend({
radioContent: [
{label: 'Red', value: 'red'},
{label: 'Blue', value: 'blue'},
{label: 'Green', value: 'green'},
{label: 'Yellow', value: 'yellow'},
]
});
For my purposes, I would like to define the array inside the template sometimes.
I tried this, but javascrip hates me:
{{Gd-radio-input content="[
{label: 'Red', value: 'red'},
{label: 'Blue', value: 'blue'},
{label: 'Green', value: 'green'},
{label: 'Yellow', value: 'yellow'},
]" value="blue"}}
Errors:
Assertion failed: The value that #each loops over must be an Array. You passed [
{label: 'Red', value: 'red'},
{label: 'Blue', value: 'blue'},
{label: 'Green', value: 'green'},
{label: 'Yellow', value: 'yellow'},
]
Uncaught TypeError: Object [
{label: 'Red', value: 'red'},
{label: 'Blue', value: 'blue'},
{label: 'Green', value: 'green'},
{label: 'Yellow', value: 'yellow'},
] has no method 'addArrayObserver'
You can generate a helper with ember g helper arr and then put this code:
{{Gd-radio-input content=(arr
(hash label='Red' value='red')
(hash label='Blue' value='blue')
(hash label='Green' value='green')
(hash label='Yellow' value='yellow')
) value="blue"}}
Explanation: the default helper already returns an array of the parameters. The hash helper generates the objects. I think the arr helper should already be in the default Template Helpers, BTW.
p.s.: Thanks to @locks on slack channel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With