Skip to content

Instantly share code, notes, and snippets.

@iDoMeteor
Last active June 27, 2016 00:42
Show Gist options
  • Save iDoMeteor/951143a18246ab7ce9a4327d1e907085 to your computer and use it in GitHub Desktop.
Save iDoMeteor/951143a18246ab7ce9a4327d1e907085 to your computer and use it in GitHub Desktop.
Psuedo-code for how to get a list of posts & one or multiple comments
<template name="posts">
<ul>
{{each post in posts}}
<li>
Post: {{post._id}} {{post.title}}
{{#if showMore}}
{{> commentList postId=post._id}}
{{else}}
First comment: {{> commentSingle postId=post._id}}
{{/if}}
</li>
{{/each}}
</ul>
</template>
<template name="commentList">
<ul>
{{#each comment in comments}}
<li>{{comment}}</li>
{{/each}}
</ul>
</template>
<template name="commentSingle">
{{comment}}
</template>
Posts = new Meteor.Collection('posts');
Comments = new Meteor.Collection('comments');
if (Meteor.isClient) {
Template.posts.onCreated(function () {
this.subscribe('posts');
});
Template.posts.helpers({
posts: () => {
return Posts.find().fetch();
},
});
Template.commentList.onCreated(function () {
this.subscribe('comments', postId);
});
Template.commentList.helpers({
comments: () => {
return Comments.find().fetch();
},
});
Template.commentSingle.onCreated(function () {
// postId is passed when called from parent template
this.subscribe('commentSingle', postId);
});
Template.commentSingle.helpers({
comment: () => {
return Comments.find().fetch();
},
});
}
if (Meteor.isServer) {
Meteor.publish('posts', () = {
Posts.find({});
});
Meteor.publish('comments', (pid) = {
Comments.find({pid: pid});
});
Meteor.publish('commentSingle', (pid) = {
Comments.find({pid: pid}, {limit: 1});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment