Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor Template events, how to get object that caused event?

Tags:

meteor

I have some code similar to the following:

In myapp.html

<template name="problems">
    <div class="problems">
        {{#each problems}}
            {{> problem}}
    {{/each}}
    </div>
</template

<template name="problem">
    <div class="problem">
        <div class="problem-text" id={{_id}}>{{text}}</div>
    </div>
</template> 

In myapp.js

Template.problem.events = {
'click .problem-text' : function () {

        var user_id = Session.get('user_id');

        // how to get problem_id of clicked item?
        Router.gotoProblem(user_id, problem_id);    
    }
};

In this situation I want to get the id of the that matched .problem-text and was clicked.

I would like to know the "object" that generated the event? How do I do this?

like image 882
Josh Petitt Avatar asked Apr 16 '12 05:04

Josh Petitt


2 Answers

The selected answer for this question will ONLY get the _id , and that too if _id is used in templates.

So better use, event.target, that will give COMPLETE object. So that can be used with jQuery or MooTools.

Template.top_nav.events({
'mousedown, .nav li a': function(evt){
  console.log('the class of object that was clicked is ' + $(evt.target).attr("class"));
 }
})
like image 106
Hitesh Joshi Avatar answered Oct 16 '22 06:10

Hitesh Joshi


Try this:

Template.problem.events = {
'click .problem-text' : function () {

        var user_id = Session.get('user_id');

        // how to get problem_id of clicked item?
        Router.gotoProblem(user_id, this._id);    
    }
};
like image 30
Thomas Lomas Avatar answered Oct 16 '22 07:10

Thomas Lomas