Define array inside template for handlebars/ember?

送分小仙女□ 提交于 2019-12-06 02:11:16

It isn't javascript that hates you, it's handlebars/helper. When you bind the content using the inline string it doesn't convert it to an array for you.

You could add some sort of contentString value that would convert it back from a string to an array and set it on the content.

{{Gd-radio-input contentString="[
    {label: 'Red', value: 'red'},
    {label: 'Blue', value: 'blue'},
    {label: 'Green', value: 'green'},
    {label: 'Yellow', value: 'yellow'},
  ]" value="blue"}}


GdRadioInput = Em.Componenet.extend({
  watchContentString: function(){
    var cs = this.get('contentString');
    if(cs){
      this.set('content', eval(cs));
    } 
  }.on('init')
});

*Note, I'm not really recommending using eval, I'm just lazy.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!