I'm updating a personal project where I used the ember.js version 0.9.x.
So a new version was released and I have a problem related with ember action.
I have the following html code:
<li><a href="#" id="startApp" {{action activateView target="view"}}> Home</a> <span class="divider">|</span></li>
where, when I click its call this function activateView:
activateView: function(event, context) { console.log(event); }
but the event and the context are undefined. I've already tried this.context and it returns undefined.
The main idea its obtain the id of the link when the user click.
I know about routes and the handlebar helper link to, but I really need that id for other things,
By default, the {{action}} helper listens for click events and triggers the action when the user clicks on the element. You can specify an alternative event by using the on option. <p> <button {{action "select" post on="mouseUp"}}>✓</button> {{post.
ember-modifier. This addon provides an API for authoring element modifiers in Ember. It mirrors Ember's helper API, with variations for writing both simple function-based modifiers and more complicated class-based modifiers.
In Ember 2...
Inside your action you always have access to the Javascript event
object which has the DOM element e.g.
actions: { myAction() { console.log(event.target) // prints the DOM node reference } }
The event is not passed using the action helper. If you really want the event object, you need to define a view and use the click
event:
App.MyLink = Em.View.extend({ click: function(e) { } });
and then:
<li>{{view App.MyLink}}</li>
but requiring access to the dom event is a rare case, because you can pass arguments to {{action}}
. In your case:
<li><a href="#" id="startApp" {{action activateView "startApp" target="view"}}> Home</a> <span class="divider">|</span></li>
and in the event:
activateView: function(id) { console.log(id); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With