Complex publish function


#1

Hi there, i have posted a question on StackOverFlow and seems like getting not much attention as most of the meteor community lives here. still I posted there so that other MongoDB expert could come into rescue.

Would you please take a look on that?


#2

Honestly, I’m kind of in the normalization boat on this one. Having dynamically changing, nested values is going to make things tougher than they need to be, in my opinion. Which is why you’re running into this issue. Your data isn’t complex, yet you already need a complex publication. That says to me that your structure needs to be re-worked.


#3

I wholeheartedly agree with @vigorwebsolutions

If you really need to go down this route, it actually is possible with the added/changed/remove API in Meteor.publish functions.

Meteor.publish('game', function(id){
  let handle = Games.find(id, {fields:yourFields}).observeChanges({
    added: function(id,fields){
      fields.players = fields.players.map(function(player){
        return player.user._id === this.userId ?
               player : 
               _.omit(player, 'hand');
      }
      this.added(...)
    },
    changed: function(id,fields){
      // you'll have to write some very similar code here 
      // to ensure only your hand's changes are sent.
      this.changed(...)
    },
    removed: function(id){
      this.removed(...)
    }
  })
  // clean up the handle here.
})