Meteor 从 Blaze 模板组件获取 ObjectId 到 .js

Meteor get ObjectId from Blaze template component to .js

我创建了一个动态 table,每个会话的行值可能会发生变化。 在最后一列中,我提供了一个 5 星评级等级,以便用户对每个显示的行进行评级,我得到正确的值,此外我需要知道用户对哪个 Id/row 进行了评级(因为这些是动态变化的)。 html 如下所示,其中在 starsRating 组件中传递了 product=this(表示已评级的行):

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

和对应的.js块

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

虽然在 运行 时我得到了 undefined,我怎样才能正确地得到它?

如果我理解正确,那么您的意图是在评级时使用该 jQuery 代码。如果是这样,那么我认为你的做法是错误的。 Blaze 对 handling events in templates.

有适当的支持

像这样的东西应该可以工作:

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

在这些事件处理程序中,this 是模板的当前数据上下文。

你只是不需要传递任何东西来获取 js 文件中事件的数据。 HTML 文件

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

JS文件

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

可以直接得到属性