Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we get the original event in ember's action

Tags:

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,

like image 786
Juan Jardim Avatar asked May 11 '13 17:05

Juan Jardim


People also ask

What is action in Ember?

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.

What is Ember modifier?

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.


2 Answers

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     } } 
like image 142
anthonygore Avatar answered Sep 22 '22 06:09

anthonygore


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);               } 
like image 37
Teddy Zeenny Avatar answered Sep 20 '22 06:09

Teddy Zeenny