ember.js: action handler no longer has access to original event
If I define an action in a view:
{{action handleClick}}
The method that handles the action is no longer provided a reference to an event. In the past an the event was the first argument and the context was accessible in the event object. Now only the context is passed as the first argument. Although this is great in many situations, access to the original event is required in some situations.
About this issue
- Original URL
- State: closed
- Created 11 years ago
- Comments: 17 (9 by maintainers)
I think another way to get the event is to use the action helper as a parameter for the bare javascript handler.
Now the action
namewill get 2 parameters. The first being ‘param’ and the second the event. But you still have to handleevent.stopPropagation()andevent.preventDefault()which are normally handled by the action helper.Could we not have a named argument
passEventAsLastArgument=truethat will add the event to the arguments for the action if it is set? Seems like overkill to have to use the above or make another component each time you would just like the javascript event.@caffinatedmonkey @mixonic I may be able to explain a use case
In a component I have a dataset and use an {{#each}} to loop through it. When I click on one of the datum I want to send an action to the component but I need that specific element clicked on to perform some logic (say hiding / masking the rest of the dataset)
Inside of a standard template I am able to do something like this (this is from an older app that is still on 1.08) there may be a new way of doing this. But this is what I expected to do inside the component I’m writing (on a 1.13 app)
where
thisis the actual DOM element andlinkis the item being iterated through. However in the component I’m writing today (1.13)thisis referring to the component itself. Am I not doing this the ember way? Should I have nested components for the things being clicked on?Update I was able to get it to work by making the element that I want to click on a sub component. Inside that components controller I implemented the click event like so
This still doesn’t seem like the ember way but I was having issues with
sendAction. Hopefully this helps someone googling in the future@realityendshere you are correct. If you want to do DOM event handling, you should do that via a view’s event handlers, not via
{{action}}.Remember that events bubble up through the view hierarchy (via normal DOM bubbling), so you can simply register your event handlers on the parent view and things will work as expected.