Meteor observeChanges返回多个光标

时间:2016-01-08 05:30:38

标签: meteor meteor-publications

出版商:

Meteor.publish('market', function(limit) {
  let self = this;

  Markets.find({}, {
    limit: limit
  }).observeChanges({
    added: function(id, market){
      self.added("market", id, market);

      let country = findCountry(market);
      self.added("countries", country._id, country);
    }
  });

  return self.ready();

});

以上发布商工作正常。我的问题是上述发布商发布market和相关的countries光标。市场游标有限制。现在我想发布带有限制的市场光标,而observeChanges中的country应无限制地运行。

所以我写得像

Meteor.publish('market', function() {
  let self = this;

  let markets = Markets.find({}, {
    limit: limit
  });

  Markets.find().observeChanges({
    added: function(id, market) {

      let country = findCountry(market);
      self.added("countries", country._id, country);
    }
  });

  return [self.ready(), markets]; // How to publish multiple cursors??

});

如何发布多个游标?

1 个答案:

答案 0 :(得分:0)

看起来好用reywood:publish-composit包。在添加,更新或删除新记录时,它会处理相关文档的所有逻辑。

我不确定findCountry函数中的逻辑是什么,但我想它会是这样的:

Meteor.publishComposite('market', {
    find: function() {
        return Markets.find({}, {limit: limit});
    },
    children: [
        {
            find: function(market) {
                return Countries.find({countryCode: market.countryCode});
            }
        }
    ]
});