Wojciech Bednarski
Wojciech Bednarski

Reputation: 6373

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?

Upvotes: 4

Views: 3902

Answers (1)

danii
danii

Reputation: 5703

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!

Upvotes: 9

Related Questions