繁体   English   中英

流星:所有Meteor.method的访问变量

[英]Meteor: Access variable across all Meteor.method's

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

当玩家2连接时,我在Meteor.method对Game集合进行更新。 但是,当我在另一个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
        // ??
    }
});

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM