如何在 Meteor 中发布集合的视图/转换? [英] How to publish a view/transform of a collection in Meteor?

查看:10
本文介绍了如何在 Meteor 中发布集合的视图/转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个收藏

var Words = new Meteor.Collection("words");

并发布:

Meteor.publish("words", function() {
    return Words.find();
});

这样我就可以在客户端上访问它.问题是,这个集合会变得非常大,我只想发布它的转换.例如,假设我想发布一个名为num words by length"的摘要,它是一个整数数组,其中索引是单词的长度,项目是该长度的单词数.所以

so that I can access it on the client. Problem is, this collection is going to get very large and I just want to publish a transform of it. For example, let's say I want to publish a summary called "num words by length", which is an array of ints, where the index is the length of a word and the item is the number of words of that length. So

wordsByLength[5] = 12;

表示有 12 个长度为 5 的单词.在 SQL 术语中,它是对原始数据集的简单 GROUP BY/COUNT.我正在尝试在客户端上制作一个模板,它会说

means that there are 12 words of length 5. In SQL terms, it's a simple GROUP BY/COUNT over the original data set. I'm trying to make a template on the client that will say something like

  • 你有 N 个长度为 X 的单词

对于每个长度.我的问题归结为我有表格 A 的数据,我想发布转换后的版本 B".

for each length. My question boils down to "I have my data in form A, and I want to publish a transformed version, B".

推荐答案

UPDATE 您可以像这样转换服务器上的集合:

UPDATE You can transform a collection on the server like this:

Words = 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 = Words.find().observe({
      added: function (document) {
      self.added('collection_name', document._id, transform(document));
    },
    changed: function (newDocument, oldDocument) {
      self.changed('collection_name', oldDocument._id, transform(newDocument));
    },
    removed: function (oldDocument) {
      self.removed('collection_name', oldDocument._id);
    }
  });

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

  self.ready();

});

这篇关于如何在 Meteor 中发布集合的视图/转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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