将集合中的文档发布到流星客户端,这取决于另一个集合中是否存在特定文档(关系发布) [英] Publish documents in a collection to a meteor client depending on the existence of a specific document in another collection (publish-with-relations)

查看:75
本文介绍了将集合中的文档发布到流星客户端,这取决于另一个集合中是否存在特定文档(关系发布)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个收藏夹

  1. 要约(相关字段:_ id )
  2. ShareRelations(相关字段: receiverId offerId )
  1. Offers (relevant fields: _id)
  2. ShareRelations (relevant fields: receiverId and offerId)

,我只想向已登录的用户发布与他共享的商品.

and I'd like to publish only Offers to the logged in user which have been shared to him.

实际上,我是通过使用一个辅助数组(visibleOffers)来实现的,该数组通过为每个ShareRelations循环进行填充,并稍后在Offer.find中将该数组用作$ in选择器.

Actually, I'm doing this by using a helper array (visibleOffers) which I fill by looping for each ShareRelations and use this array later on the Offers.find as $in selector.

我想知道这是否可能是流星方法,还是我可以用更少和/或更漂亮的代码来做到这一点?

I wonder if this might be the meteor way to do this, or if I could do with less and/or prettier code?

我发布优惠的实际代码如下:

My actual code to publish the Offers is the following:

Meteor.publish('offersShared', function () {
  // check if the user is logged in
  if (this.userId) {
    // initialize helper array
    var visibleOffers = [];
    // initialize all shareRelations which the actual user is the receiver
    var shareRelations = ShareRelations.find({receiverId: this.userId});
    // check if such relations exist
    if (shareRelations.count()) {
      // loop trough all shareRelations and push the offerId to the array if the value isn't in the array actually
      shareRelations.forEach(function (shareRelation) {
        if (visibleOffers.indexOf(shareRelation.offerId) === -1) {
          visibleOffers.push(shareRelation.offerId);
        }
      });
    }
    // return offers which contain the _id in the array visibleOffers
    return Offers.find({_id:  { $in: visibleOffers } });
  } else {
    // return no offers if the user is not logged in
    return Offers.find(null);
  }
});

此外,实际的解决方案还有一个缺点,就是如果正在创建新的共享关系,则客户端上的优惠"集合不会立即用新的可见出价进行更新(阅读:需要重新加载页面.但是我不是)确定是由于这种发布方法还是由于其他一些代码导致这种情况不是这个问题,因为这个问题.)

Furthermore, the actual solution has the downside that if a new share relations is being created, the Offers collection on the client doesn't get updated with the newly visible offer instantly (read: page reload required. But I'm not sure if this is the case because of this publish method or because of some other code an this question is not primary because of this issue).

推荐答案

您正在寻找的是反应式联接.您可以通过直接使用publish函数中的观察值或使用库为您完成此操作来完成此操作.流星核心有望在某个时候拥有 join库,但是直到那时我d建议使用 publish-with-relations .看一下文档,但是我认为您想要的发布功能看起来像这样:

What you are looking for is a reactive join. You can accomplish this by directly using an observe in the publish function, or by using a library to do it for you. Meteor core is expected to have a join library at some point, but until then I'd recommend using publish-with-relations. Have a look at the docs, but I think the publish function you want looks something like this:

Meteor.publish('offersShared', function() {
  return Meteor.publishWithRelations({
    handle: this,
    collection: ShareRelations,
    filter: {receiverId: this.userId},
    mappings: [{collection: Offers, key: 'offerId'}]
  });
});

这应该为用户反应性地发布所有ShareRelations和所有关联的Offers.希望两个都不会有问题.

This should reactively publish all of the ShareRelations for the user, and all associated Offers. Hopefully publishing both won't be a problem.

PWR是一个非常合法的软件包-我们中的一些人在生产中使用它,而 Tom Coleman 对此有所贡献.我唯一要提醒您的是,在撰写本文时,大气层中的当前版本(v0.1.5)中存在一个错误,该错误会导致相当严重的内存泄漏.在不知所措之前,请参阅我的博客文章有关如何运行更新的本地副本.

PWR is a pretty legit package - several of us use it in production, and Tom Coleman contributes to it. The only thing I'll caution you about is that as of this writing, the current version in atmosphere (v0.1.5) has a bug which will result in a fairly serious memory leak. Until it gets bumped, see my blog post about how to run an updated local copy.

发现流星博客在反应联接上有一篇非常好的文章,我强烈建议阅读

The discover meteor blog has an excellent post on reactive joins which I highly recommend reading.

这篇关于将集合中的文档发布到流星客户端,这取决于另一个集合中是否存在特定文档(关系发布)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆