Reputation: 955
I'm creating a little game, and I want to display the name of the opponent when one player is waiting for an action. For displaying my opponents name I have the following function.
Template.Details.helpers({
opponentName: function() {
var opponentId = _.without(this.players, Meteor.userId)[0];
var oppDoc = Meteor.users.findOne({_id: opponentId});
return oppDoc;
})
In my template
<template name='opponent'>
<h3> {{opponentName.username}} </h3>
</template>
This will return the correct username for the first time. However, the username will not change after this. I think this is because of my var opponentId will not change.
I was thinking of a way to make this reactive. So when the reactieve data source (Meteor.userId) changes a function with opponentId will rerun and return a new userId.
What is the correct way to do this, and do I still do this in my Template helper?
EDIT:
I also tried the following:
Template.Details.helpers({
opponentName: function() {
var id = Games.findOne({_id: this._id}, {isCreating: 1})
var opponentId = _.without(this.players, id)[0];
var oppDoc = Meteor.users.findOne({_id: opponentId});
return oppDoc;
})
My idea was that, because isCreating is updating id is reactive, and therefore will update everything. But this doesn't work either.
Upvotes: 0
Views: 163
Reputation: 36900
Meteor.userId()
needs to be called as a function in order for it to register reactive dependencies properly. So just add those brackets at the end and you should be all set.
The other thing that might be causing an issue is the value of this.players
. Is that being updated reactively when the opponents change?
Upvotes: 1