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

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

问题描述

我已经收藏了

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

并将其发布:

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

,以便我可以在客户端上访问它.问题是,这个收藏将会变得很大,我只想发布它的一个变形.例如,假设我要发布一个摘要,即按长度计的单词数",它是一个整数数组,其中索引是单词的长度,项目是该长度的单词数.所以

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 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天全站免登陆