Vlad Tarniceru
Vlad Tarniceru

Reputation: 741

Meteor JS - Same template which uses multiple collections

I have 2 collections which go as:

Posts = new Mongo.Collection('posts');
Pinned = new Mongo.Collection('pinned');

I want to be able to do this:

{{> posts}}

<template name='posts'>
    {{#each posts}}
        <!-- code goes here -->
    {{/each}}
    {{#each pinned}}
        <!-- code goes here -->
    {{/each}}
</template>

So far I've seen that I can't use data from 2 different collections in the same template. Any ideas how can I achieve this?

Thanks in advance.

Upvotes: 0

Views: 54

Answers (1)

Ankit
Ankit

Reputation: 1128

I assume you have used publish and subscribe with iron-router. You can achieve your objective in either of the following ways:

Method 1:

Html

<template name="posts">
    {{#each posts}}
         <!-- code here-->
    {{/each}}

    {{#each pinned}}
        <!-- code here -->
     {{/each}}
</template>

Js

Template.posts.helpers({
      posts : function(){
          return  Posts.find().fetch();
      },
      pinned : funcion(){
           return Pinned.find().fetch();
      }

});

Method 2:
Html

<template name="posts">
     {{#each posts}}
           <!-- code here -->
     {{/each}}

     {{> pinned}}

</template>

<template name="pinned">
     {{#each pinned}}
           <!-- code here -->
     {{/each}}
</template>

Js

Template.posts.helpers({
       posts : function(){
      return  Posts.find().fetch();
});
Template.pinned.helpers({
       pinned : function(){
      return  Pinned.find().fetch();
  });

Upvotes: 1

Related Questions