Meteor Collection转换:它是在服务器上还是在客户端上完成的?或者取决于 [英] Meteor Collection Transform: is it done on the server or on the client? or it depends

查看:133
本文介绍了Meteor Collection转换:它是在服务器上还是在客户端上完成的?或者取决于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用transform从集合中创建一个虚拟字段。但是,我添加的新字段(在转换函数中)正在向返回的文档添加相当多的数据。

I want to use transform to make a "virtual field" out of a collection. However, the new field I'm adding (within the transform function) is adding quite a bit of data to the returned document.

如果转换发生在客户端内,这很好。如果在服务器端完成,则会出现带宽问题。

This is fine if the transform is taking place inside the client. If it is done on server-side, then there will be bandwidth concerns.

所以我想知道转换是在服务器上还是在客户端上完成的,或者取决于我如何查找/获取文档?

So I'm wondering if the transform is done on the server, or on the client, or it depends on how I find/fetch the document?

推荐答案

更新:可以这样做服务器上的转换。

UPDATE: It's possible to do a transform on the server.

您可以在客户端进行如下转换:

You can have a transform on the client like this:

return YourCollection.find({}, {transform: function (doc) {
   doc.test = true;
   return true;
}});

Meteor忽略转换对已发布的查询(来自 Meteor.publish )。客户端看到文档就好像转换不存在一样。

Meteor ignores transform on queries that are published (from within Meteor.publish). The client sees the document as if the transform didn't exist.

如果您想在服务器上使用转换,您可以这样做:

If you would like to use transforms on the server you can do this:

YourCollection = new Mongo.Collection("collection_name"); 

Meteor.publish("yourRecordSet", function() {

  //Transform function
  var transform = function(doc) {
    doc.date = new Date();
    return doc;
  }

  var self = this;

  var observer = YourCollection.find().observe({
      added: function (document) {
      self.added('collection_name', document._id, transform(document));
    },
    changed: function (newDocument, oldDocument) {
      self.changed('collection_name', newDocument._id, transform(newDocument));
    },
    removed: function (oldDocument) {
      self.removed('collection_name', oldDocument._id);
    }
  });

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

  self.ready();

});

这篇关于Meteor Collection转换:它是在服务器上还是在客户端上完成的?或者取决于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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