this._id 无法访问 Meteor js 中的 Iron 控制器。如何获取ID?

this._id is not accessible to Iron controller in Meteor js. How to get the id?

我的 dashboard.html

里有这个
{{#each todos}}
  <li class="list-group-item">
      {{ task }}
      <button class="delete"> Delete</button>
  </li>
{{/each}}

在控制器中dashboard.js我有这个

DashboardController.events({
  'click .delete': function () {
    Meteor.call('ToDos.delete',this._id);
  }
});

在没有 Iron Controller 的情况下,我可以使用 this._id 访问事件中的集合 ID,但使用此设置它具有空值。任何想法如何在控制器中获取待办事项集合的 ID?

执行以下操作。

DashboardController = RouteController.extend({
    template: 'layout',
    data: function () { return SomeCollection.findOne(); },
    waitOn: function() { return Meteor.subscribe('someCollection') }
});

DashboardController.events({
  'click .delete': function () {
   console.log(this.data())
   console.log(this.data()._id)//not sure if this works.
    Meteor.call('ToDos.delete',this.data());
  }
});

这里的关键是我们不需要 {{#each}},您应该使用数据函数用数据填充模板。

如果您按照以下步骤进行操作,它将起作用。

Template.example.events({
'click .delete': function () {
       console.log(this.data()) //undefined
       console.log(this._id)//id
        Meteor.call('ToDos.delete',this._id);
      }
})