在 Meteor 中合并集合 [英] Merging collections in Meteor

查看:15
本文介绍了在 Meteor 中合并集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,您希望在单个社交新闻提要上显示多个收藏集,例如 PostsUsers(新注册).您将如何在按创建日期排序的列表中被动地显示两者?

Imagine you have multiple collections you want displayed on a single social news feed, for example Posts and Users (new signups). How would you reactively display both in a list sorted by creation date?

您分别发布实体,然后在客户端中,按日期对每个实体进行排序,然后将它们合并为一个已排序的数组,然后将其显示在 {{#each}} 迭代器中.问题: AFAIK 这需要将反应式游标展平为静态数组,因此现在页面不会更新.(也许有一种方法可以让页面在任何集合更改时重新计算此数组,从而挽救这种方法?)

You publish the entities separately, and in the client, sort each of them by date, and merge them into a single sorted array, which you then display in an {{#each}} iterator. Problem: AFAIK this requires flattening the reactive cursors into static arrays, so now the page won't update. (Perhaps there's a way of making the page recalculate this array when any collection changes, salving this approach?)

您创建了一个新集合,例如 FeedItems.当创建一个新的 PostUser 时,您还创建了一个新的 FeedItem 并将相关信息复制到其中.在客户端中显示项目现在非常简单.问题:现在规范对象和它们的 FeedItem 版本之间没有反应性,所以如果有人更改他们的名字、删除帖子等,这不会反映在提要中.(也许有一种方法可以在集合之间创建反应性来挽救这种方法?)

You create a new collection, say FeedItems. When a new Post or User is created, you also create a new FeedItem and copy the relevant information into it. Displaying the items in the client is now very straightforward. Problem: Now there is no reactivity between the canonical objects and the FeedItem versions of them, so if someone changes their name, deletes a post, etc., this won't be reflected in the feed. (Perhaps there's a way of creating reactivity between collections to salvage this approach?)

也许有一些方法可以保留现有的集合,但创建一个额外的 'newsFeed' 出版物,以某种方式合并它们.然而,我还没有看到任何这样做的方法.我在文档中看到您可以发布一系列集合,但 AFAIK 这相当于一次发布一个相同的集合.

Perhaps there's some way of sticking with the existing collections, but creating an additional 'newsFeed' publication which would somehow merge them. I haven't seen any way of doing this, however. I see in the docs that you can publish an array of collections, but AFAIK this is equivalent to publishing the same collections one at a time.

这些方法之一是否在正确的轨道上?或者还有其他我没有想到的吗?

Is one of these approaches on the right track? Or is there another I haven't thought of?

推荐答案

到目前为止,最简单的解决方案是在客户端的模板助手中合并它们.例如:

By far the easiest solution is to merge them on the client in a template helper. For example:

Template.dashboard.helpers({
  peopleAndPosts: function() {
    var people = People.find().fetch();
    var posts = Posts.find().fetch();
    var docs = people.concat(posts);
    return _.sortBy(docs, function(doc) {return doc.createdAt;});
  }
});

和模板(假设人和帖子有一个name):

and the template (assuming people and posts have a name):

<template name="dashboard">
  <ul>
    {{#each peopleAndPosts}}
    <li>{{name}}</li>
    {{/each}}
  </ul>
</template>

这个是响应式的,因为助手是一个响应式上下文,因此对PeoplePosts 的任何更改都将导致重新计算返回的数组.请注意,由于您没有返回游标,因此对任一集合的任何更改都将导致整个集合再次呈现.如果返回数组的长度比较短,这不会有什么大不了的.

This will be reactive because the helper is a reactive context so any changes to People or Posts will cause the returned array to be recomputed. Be aware that because you are not returning a cursor, any changes to either collection will cause the entire set to render again. This won't be a big deal if the length of the returned array is relatively short.

这篇关于在 Meteor 中合并集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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