Meteor:跨所有 Meteor.method 的访问变量



我对流星很陌生,但我只是做了一个简单的回合制多人游戏。

当玩家 2 连接时,我会在Meteor.method内更新游戏集合。但是,当我在另一个Meteor.method想要获得该更新时,我需要再次Games.find()它,以获取该更新的值。

如何存储当前的游戏实例,以便我可以使用所有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
        // ??
    }
});

我尝试将当前游戏保存在方法对象中,但后来它没有反应。不知道下一步该怎么做。

无需从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 });
});

最新更新