在流星中合并收藏 [英] Merging collections in Meteor
问题描述
想象一下,您要在单个社交新闻源上显示多个收藏集,例如Posts
和Users
(新注册).您将如何在创建日期排序的列表中反应性地显示两者?
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
.创建新的Post
或User
时,您还将创建一个新的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>
此将是响应式的,因为帮助器是响应式上下文,因此对People
或Posts
的任何更改都将导致重新计算返回的数组.请注意,由于您没有返回游标,因此任何一个集合的任何更改都将导致整个集合再次呈现.如果返回的数组的长度相对较短,这没什么大不了的.
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.
这篇关于在流星中合并收藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!