Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling action parameter in Ember.js

In template:

<button {{action someAction someParameter}}>Some Action</button>

In controller:

someAction: function (e, someParameter) {
    console.log(e, someParameter);
}

someParameter is undefined as well as e where I excepted to be event object.

How to pass parameter to action? If not possible does it mean I need to create Ember.View to handle action with parameter?

like image 683
Wojciech Bednarski Avatar asked Jan 13 '23 18:01

Wojciech Bednarski


1 Answers

The only problem I see in your code is that the event object is not passed to the function in the controller when using the {{action}} helper. Regardless of that, your code should log the value of someParameter to the console. If you are getting two undefined maybe someParameter is not within the context of the template, or it is undefined.

Make sure someParameter is there and holds the correct value, for example:

Template:

<button {{action someAction someParameter}}>Some Action (param: {{someParameter}} )  </button>

If the value doesn't show up, try view.someParameter depending on how you are rendering the template, if you show your code we might be able to help you more.

On the controller:

someAction: function (someParameter) {
  console.log(someParameter);
}

Hope this helps!

like image 157
danii Avatar answered Jan 21 '23 22:01

danii