【问题标题】:How to access current plain-object I'm iterating over an arrays of in Meteor template如何访问我正在迭代 Meteor 模板中的数组的当前普通对象
【发布时间】:2016-02-18 19:08:52
【问题描述】:

我有一个普通对象数组(由助手返回,但与问题无关):

Things = [{
  a: 'some thing'
}, {
  a: 'more thing'
}];

然后我在 Meteor 模板中对其进行迭代:

{{#each Things}}
  <button>{{a}}</button>
{{/each}}

在集合的情况下,它是一个从事件到上下文的超级方便的快捷方式:

Template.whatever.events({
  'click button': () => console.log(this); // e.g., {a: 'some thing'}
});

但在普通对象的情况下,this 快​​捷方式不起作用,它引用 window 对象。

如何从偶数处理程序中提取作为 each 块上下文的数组项,而不显式向可迭代对象添加一些唯一标识符?

【问题讨论】:

    标签: arrays meteor handlebars.js


    【解决方案1】:

    这是因为您使用的 arrow es15 语法。箭头语法在函数中保留 this 的上下文。我不确定为什么它适用于集合而不是对象数组,但如果您使用function 表示法,它会起作用。

    Template.whatever.events({
      'click button': function () {
           console.log(this);  // e.g., {a: 'some thing'}
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      • 2021-09-06
      • 2020-10-17
      • 1970-01-01
      相关资源
      最近更新 更多