Reputation: 1533
Im having it difficult to understand how when i add events to a certain template, "this" know what document and collection to refer to.
Example:
Template.game.events({
"click a.one-plus": function(event, template){
event.preventDefault();
Games.update({_id:this._id}, {$inc:{
"teams.0.score":1
}});
},
});
Upvotes: 0
Views: 38
Reputation: 280
this
will refer to the context associated with the template (or template fragment) where the event is triggered.
Let's say your template is like this:
<template name='myTemplate'>
<button id='myButton'/>
{{#each myList}}
<button class='mySecondButton'/>
{{/each}}
</template>
And you've defined
Template.helpers({
myList() {
return [1,2,3];
}
});
Then the context for a click
on myButton
will contain this.myList()
. (If you use a Router
, you will probably use a different way to 'bind' data to your template.)
Be careful though: in the each
iteration, the context changes to the current iterator element. So this
will be respectively 1
, 2
and 3
in this case, so the three .mySecondButton
buttons will see a different context. each
changes the context, so does with
.
Upvotes: 1