【问题标题】:Meteor get ObjectId from Blaze template component to .jsMeteor 从 Blaze 模板组件获取 ObjectId 到 .js
【发布时间】:2020-03-19 18:43:34
【问题描述】:

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

<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');

运行时出现不确定,如何正确获取?

【问题讨论】:

    标签: javascript meteor meteor-blaze


    【解决方案1】:

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

    这样的事情应该可以工作:

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

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

    【讨论】:

      【解决方案2】:

      你不需要传递任何东西来获取 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);
          }
      });
      

      可以直接获取属性

      【讨论】:

        猜你喜欢
        • 2017-08-20
        • 2016-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-24
        • 1970-01-01
        • 1970-01-01
        • 2019-02-02
        相关资源
        最近更新 更多