Merging collections in Meteor

前端 未结 3 1825
被撕碎了的回忆
被撕碎了的回忆 2020-12-10 15:20

Imagine you have multiple collections you want displayed on a single social news feed, for example Posts and Users (new signups). How would you rea

3条回答
  •  春和景丽
    2020-12-10 16:10

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

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

    
    

    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.

提交回复
热议问题