Search code examples
ember.jsember-data

Can I pass a more than one value in a one option from x-select addon on ember?


I would like to pass a more than one value in a one option from a x-select addon in ember. Is that possible? For example,

  {{#each model as |model|}}
     {{#xs.option value1=model.name value2=model.anotherAttributeName}
       {{model.name}} 
     {{/xs.option}}
  {{/each}}

Any suggestions and answers are much appreciated, thanks!


Solution

  • No, that is not possible. option-component wraps a native <option> tag. option component has following attributeBindings:

    'selected', 'name', 'disabled', 'value', 'title'
    

    Just pass the whole model to option component as value.

    {{#x-select value=selected onChange=(action "selectOption") as |xs|}}
      {{#each model as |model|}}
         {{#xs.option value=model}}
           {{model.name}} 
         {{/xs.option}}
      {{/each}}
    {{/x-select}}
    

    At your action you can access your whole model.

      actions: {
        selectOption(value) { // value is a reference to the selected model
          console.log(value.name, value.anotherProperty);
          this.set('selected', value);
        }
      }