Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass in more than one parameter to the onChange action for the select element in Ember2.3

I have a select element within a loop, and the action I want to be triggered needs to have two parameters: the selected option of the select element and the item we are looping with. My code looks like so:

{{#each loopItems as |loopItem|}}
 <select onChange={{action 'fireThis' loopItem target.value }}> 
  {{#each loopItem.subItems as |subItem|}}
    <option value={{subItem.id}}>{{subItem.value}}</option>
  {{/each}}
 </select>
{{/each}}

And my fireThis action is like so:

fireThis:function(loopItem, value){
 //Do something here with both. 
 // like loopItem.set('thisProperty', value);
}

So, in my particular case, the number of subItems is dynamic, and I need to use the selected subItem with the loopItem it is currently under.

Barring refactoring into components (right now, I cannot do that), how would I pass in more than one parameter to the action fired on 'onChange'?

I have tried:

<select onChange={{action 'fireThis' value ="loopItem target.value" }}> 
<select onChange={{action 'fireThis' value ="loopItem target.value" }}>
<select onChange={{action 'fireThis' loopItem value="target.value"}}>
<select onChange={{action 'fireThis' value="target.value" loopItem}}>

For all of the above, both my params in the function are undefined (or cause another error). The only one that works is:

<select onChange={{action 'fireThis' value="target.value"}}>

But that only gives me the subItem, not the loopItem.

Unfortunately, the loopItems are newly created Ember Objects, void of any ID parameters, so I cannot give each select element a unique ID either. The ability to pass more than one parameter would pretty much solve my entire problem.

like image 845
Darshan Avatar asked Feb 11 '16 19:02

Darshan


1 Answers

A bit late, but the answer found here works: ember 2 parameters in a action of a select menu.

For OP's solution you wrap it like so:

<select onChange={{action (action 'fireThis' loopItem) value="target.value"}}>

like image 51
mrchaarlie Avatar answered Oct 31 '22 05:10

mrchaarlie