Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define array inside template for handlebars/ember?

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' 
like image 353
Himmators Avatar asked Apr 12 '26 01:04

Himmators


1 Answers

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

like image 177
morhook Avatar answered Apr 13 '26 13:04

morhook



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!