Passing parameter


#1

Hi guys, i have two buttons so when i click each button i should know which button i have clicked so how can i do it in ember js (button is not component type just normal button


#2

I don’t know if it is THE correct way to do it, but one relatively simple way would of course be to create a component out of it. If you just let ember cli generate the component for you, you can easily pass in some value to the component as you use it in your templates.

{{#my-button param="firstButton"}}First button{{/my-button}}
{{#my-button param="secondButton"}}Second button{{/my-button}}

In the component code you can then just do something like

var passedParam = this.get('param');

in an action to get the value. Of course, I don’t know if this is the correct way to go in your situation, but it could be a starting point.

Disclaimer: I am pretty much a newbie myself, but I’ve done something similar a few times.


#3

There is no need to create a component here.

Either send a different action from each button, or use a different argument for each action.

<button {{action 'action1'}}>Button 1</button>
<button {{action 'action2'}}>Button 2</button>

or

<button {{action 'action' 'button1'}}>Button 1</button>
<button {{action 'action' 'button2'}}>Button 2</button>

#4

The value of passing parameters to actions like in @alexspeller’s second example is even clearer if you consider a big loop:

{{#each tweets as |tweet|}}
  <div>{{tweet.text}}</div>
  <button {{action 'fav' tweet}}>Fav</button> 
{{/each}}
export default Component.extend({
  actions: {
    fav(tweet) {
      alert(`You faved a tweet that says ${tweet.text}`);
    }
  }
})