这个_Meteor js中的Iron控制器无法访问id.如何获取id

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

本文关键字:id 何获取 获取 访问 js Meteor 中的 Iron 控制器 这个      更新时间:2023-09-26

我的dashboard.html 中有这个

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

在控制器CCD_ 2中,我有这个

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

如果没有Iron Controller,我可以使用this._id访问事件中集合的id,但使用此设置,它的值为null。你知道如何在控制器中获取集合todo的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}},您应该使用data函数来用数据填充模板。

如果你按照下面的步骤做,它就会起作用。

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