2016-02-27 100 views
0

我對Meteor很新穎,但我只是做了一個簡單的回合制多人遊戲。流星:所有Meteor.method的訪問變量

當玩家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

當然。添加它。 –

回答

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 s。挑一個,並與它一起去。陣列形式更簡單,在這種情況下,您的發佈功能將爲:

Meteor.publish('myGames',function(){ 
    return Games.find({ players: this.userId }); 
});