How to link to an object with a click in Ember.js

Sorry if this is a question about Nob, I am new to Ember.js and trying to understand some things. In Ember.js, let's say I have a list of objects in an array controller and rendering a view for each specific object, if the user clicks on a DOM element belonging to a specific object, how can I determine which object the element was clicked on?

Example

ul#mainChat {{#each chat in App.chatController}} {{#view App.MainChatView tagName="li"}} li | Example {{/view}} {{/each}} 

If I have 5 objects in the chat controller array and 5 li, the user clicks on one, how can I determine which object belongs to.

Thanks!

+4
source share
1 answer

You must enable the action for this element and pass the action to the context that you can access through the event.context action in the target.

For instance.

 {{#each item in items}} <li {{action viewItem item target="view"}}>{{name}}</li> {{/each}} 

Then in your view (or wherever you set the goal of the action, it depends on what you are doing):

 viewItem: function(event) { var item; item = event.context; //do what you want with the item } 
+5
source

All Articles