Hi, in the event code below from the simple-todos.js example,
this._id some how returns the database _id.
Can anyone explain how the _id got attached to the task and how its available in the code below?
Thankyou
Template.task.events({
"click .toggle-checked": function () {
// Set the checked property to the opposite of its current value
Tasks.update(this._id, {$set: {checked: ! this.checked}});
},
"click .delete": function () {
Tasks.remove(this._id);
}
});
When you create a document in Mongo it will create an automatic ID attached to it (_id) so all tasks have ID’s .
Here you call for Tasks.update so this._id means the task you clicked on and then you pass to set to checked.
On the remove call also you call Tasks.remove(this._id) , you call to remove a task and then you pass the specific Id of the one you clicked on which is what the “this” in this case does
Not sure if I managed to explain it well, I am also new to meteor and my english is not the best.
Hi thanks for replying, I am still confused
I can only see “task” defined as:
<template name="task">
<li class="{{#if checked}}checked{{/if}}">
<button class="delete">×</button>
<input type="checkbox" checked="{{checked}}" class="toggle-checked" />
<span class="text"><strong>{{username}}</strong> - {{text}}</span>
</li>
</template>
How does the _id get added to the “task” template instance? Is it some kind of meteor magic? Wish I had a clearer Idea.
“Tasks” the Collection, does not seem automatically related to “task” to me. I’m still finding it confusing.
In the #each-loop, each context (this) is the whole document, including the id. You can do console.log(this) to see it. For example
"click .toggle-checked": function () {
console.log(this)
// Set the checked property to the opposite of its current value
Tasks.update(this._id, {$set: {checked: ! this.checked}});
},