Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

javascript - Meteor Collection Transform: is it done on the server or on the client? or it depends

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 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();

});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...