在每个文档中构建具有附加字段的响应式出版物 [英] Build a reactive publication with additional fields in each document

查看:25
本文介绍了在每个文档中构建具有附加字段的响应式出版物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个包含多个附加字段的出版物,但我既不想使用 Collection.aggregate 也不想在集合更改时丢失我的出版物更新(因此我不能只使用self. added 在其中).

I want to make a publication with several additional fields, but I don't want to either use Collection.aggregate and lose my publication updates when the collection change (so I can't just use self.added in it either).

我计划使用 Cursor.observeChanges 来实现这一点.我有两个主要限制:

I plan to use Cursor.observeChanges in order to achieve that. I have two major constraints:

  1. 我不想发布所有文档字段
  2. 我想使用一些未发布的字段来创建新的字段.例如,我有一个字段 item,我在其中存储了一个 item _id 数组.我不想发布它,但我想发布一个item_count 字段和我的字段数组的长度
  1. I don't want to publish all the documents fields
  2. I want to use some of the unpublished fields to create new ones. For example, I have a field item where I store an array of item _id. I don't want to publish it, but I want to publish a item_count field with the length of my field array

方法来了:

  1. 我计划链接查找查询.我从来没有这样做过,所以我想知道是否可能.一般(简化)查询结构如下:http://jsfiddle.net/Billybobbonnet/1cgrqouj/(我无法在此处正确显示代码)

  1. I plan to chain find queries. I never did that so I wonder if it possible. The general (simplified) query structure would be like this: http://jsfiddle.net/Billybobbonnet/1cgrqouj/ (I cant get the code properly displayed here)

基于 Meteor 文档中的计数示例,我存储了我的在变量 handle 中查询,以便在客户端取消订阅时停止更改通知:

Based on the count example in Meteor documentation, I store my query in a variable handle in order to stop the changes notification if a client unsubscribes:

self.onStop(function () {
  handle.stop();
});

  1. 我在查询之前添加了一个标志 initializing = true; 并在调用 self.ready(); 之前将它设置为 true.仅当发布已初始化时,我才使用此标志来更改我的 itemCount 变量.所以基本上,我像这样改变我的 switch:
  1. I add a flag initializing = true; before my query and I set it to true just before calling self.ready();. I use this flag to change my itemCount variable only if it is the publication is initialized. So basically, I change my switch like that:

switch (field) {
  case "item"
    if (!initializing)
      itemCount = raw_document.item.length;
      break;
  default:
}

在对我的代码进行重大更改之前,我想检查一下这种方法是否良好且可行.有人可以确认我这是否是正确的方法吗?

I wanted to check that this approach is good and possible before committing into big changes in my code. Can someone confirm me if this is the right way to go?

推荐答案

保持字段私有相对容易,即使它们是数据库查询的一部分.self. added 的最后一个参数是传递给客户端的对象,因此您可以删除/修改/删除发送给客户端的字段.

It's relatively easy to keep fields private even if they are part of the database query. The last argument to self.added is the object being passed to the client, so you can strip/modify/delete fields you are sending to the client.

这是您的小提琴的修改版本.这应该做你所要求的.(老实说,我不确定为什么你的小提琴中的 observeChanges 函数后面有任何链接,所以也许我误解了你,但看看你的其余问题,这应该是它.对不起,如果我弄错了.)

Here's a modified version of your fiddle. This should do what you are asking for. (To be honest I'm not sure why you had anything chained after the observeChanges function in your fiddle, so maybe I'm misunderstanding you, but looking at the rest of your question this should be it. Sorry if I got it wrong.)

var self = this;

// Modify the document we are sending to the client.
function filter(doc) {
  var length = doc.item.length;

  // White list the fields you want to publish.
  var docToPublish = _.pick(doc, [
      'someOtherField'
  ]);

  // Add your custom fields.
  docToPublish.itemLength = length;

  return docToPublish;                        
}

var handle = myCollection.find({}, {fields: {item:1, someOtherField:1}})
            // Use observe since it gives us the the old and new document when something is changing. 
            // If this becomes a performance issue then consider using observeChanges, 
            // but its usually a lot simpler to use observe in cases like this.
            .observe({
                added: function(doc) {
                    self.added("myCollection", doc._id, filter(doc));
                },
                changed: function(newDocument, oldDocument)
                    // When the item count is changing, send update to client.
                    if (newDocument.item.length !== oldDocument.item.length)
                        self.changed("myCollection", newDocument._id, filter(newDocument));
                },
                removed: function(doc) {
                    self.removed("myCollection", doc._id);                    
                });

self.ready();

self.onStop(function () {
  handle.stop();
});

这篇关于在每个文档中构建具有附加字段的响应式出版物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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