Meteor: Manipulate Template Data

64 Views Asked by At

This is a Meteor/Blaze question.

I have a Mongo collection called Lists, which contains list of postIds, which I would like to query and fill in with data from another collection called Posts.

Schema

Posts: [{
   _id: String,
   // post data, I want to access this from a repeated list
}]

Lists: [{
 _id: String,
 posts: [String] // of ids
}]

List Template:

{{#if posts}}   /// referring to List.posts
<div class="list-posts">
  <ul>
    {{#each posts}}

         // here I would like query for the corresponding Posts data   

    {{/each}}
  </ul>
</div>
{{/if}}

I'm not sure how to do this with Meteor. Template Helpers?

1

There are 1 best solutions below

4
On BEST ANSWER

An elegant solution would be to use the popular dburles:collection-helpers package :

Lists.helpers({
  posts: function(){
    return Posts.find({
      _id: {
        // I suggest renaming your Lists.posts field to Lists.postIds.
        $in: this.postsIds
      }
    });
  }
});

Then you'll have to make sure the corresponding posts are published along with your lists, and you'll be able to use {{#each posts}} just like in your pseudo-code.