Search code examples
javascriptmeteormeteor-blaze

Meteor get ObjectId from Blaze template component to .js


I have created a dynamic table of which the row values may change per Session. In last column I present a 5 star rating scale so that the users rates each displayed row, I get the value correctly and in addition I need to know which Id/row the user has rated (since these change dynamically). The html looks like below where product=this (indicating the row that is rated) is passed in starsRating component:

<td class="featuretablerow">
   <p>{{> starsRating mutable=true size='md' class='js-rate-image'
          id='js-rate-image' product=this }}</p>
</td>

and the corresponding .js block

var starId = $('#js-rate-image').attr('product');

I get undefined though when running it, how can I get it properly?


Solution

  • If I understand correctly, then your intention is to use that jQuery code in the event of a rating. If that is so, then I would argue you are going about that the wrong way. Blaze has proper support for handling events in templates.

    Something like this should work:

    Template.starsRating.events({
      'click': function() {
        console.log('rated product is', this.product);
      }
    });
    

    In these event handlers, this is the current data context of the template.