Chris Ian Chris Ian - 5 months ago 25
Javascript Question

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

I have this in my

dashboard.html


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


and in the controller
dashboard.js
I have this

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


Without Iron Controller I can access the id of the collection in the event using
this._id
but with this setup it has a null value. Any ideas how to get the id of the collection todo in the controller?

Answer

Do the follow.

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());
  }
});

The key here its that we don't need the {{#each}}, you should use the data function to populate the template with data.

if you do the follow it will work.

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