我对Meteor很陌生,但我刚做了一个简单的回合制多人游戏。
当玩家2连接时,我会在Meteor.method
内对游戏集合进行更新。但是当我在另一个Meteor.method
想要获得更新时,我需要再次Games.find()
,以获得更新的值。
如何存储当前的Game实例,我可以使用Meteor.method's
来访问它?
如果它是在客户端,我会使用reactive-vars
,但我想这不是一个选项?
修改
Meteor.methods({
startGame: function() {
return Games.insert({
players: [{
_id: Meteor.userId()
}]
});
},
joinGame: function(game) {
return Games.update({
_id: game._id
}, {
$set: {
endsAt: new Date().getTime() + 10000
},
$push: {
players: Meteor.userId()
}
});
},
getDataFromGame: function() {
// How can I get data from the
// game inside other Methods
// without using Games.find
// ??
}
});
我尝试将当前游戏保存在方法对象中,但之后它没有被反应。不知道接下来该做什么。
答案 0 :(得分:0)
而不是从Meteor.call()
返回游戏只是发布用户加入的游戏。
Meteor.publish('myGames',function(){
return Games.find({ players: { $elemMatch: { _id: this.userId }}});
});
然后在客户端:
Meteor.subscribe('myGames');
我应该指出,在startGame
的代码中,players
键包含一个对象数组{_id: Meteor.userId()}
,而在startGame
中,同一个键只包含一个用户数组_id
秒。选择一个并继续使用它。数组形式在这里更简单,在这种情况下,您的发布函数将是:
Meteor.publish('myGames',function(){
return Games.find({ players: this.userId });
});