Template subscription to one model in different way?

I have mongo model Posts. And two templates with different criteria.

I would like to see: in one template - posts ordered by -1, in second - posts ordered by 1. Both - limited - by 5.

So, I made simple publish (In real life it’s a little more parameters).

Meteor.publish('posts',function() {
  return Posts.find({},{limit:5,sort:{createdAt: 1}});
});

Meteor.publish('postsback',function() {
  return Posts.find({},{limit:5,sort:{createdAt: -1}});
});

And template subscription like:

Template.postone.onCreated(function(){
  this.subscribe('posts');
});

Template.posttwo.onCreated(function(){
  this.subscribe('postsback');
});


Template.postone.helpers({
  posts: function(){
    return Posts.find();
  }
});

Template.posttwo.helpers({
  postsback: function(){
   return Posts.find();
  }
});

I hade - posts equals in order and have no limits. Where I had made mistake?

Template.postone.helpers({
  posts: function(){
    return Posts.find({},{limit:5,sort:{createdAt: 1}});
  }
});

Template.posttwo.helpers({
  postsback: function(){
    return Posts.find({},{limit:5,sort:{createdAt: -1}});
  }
});

So. No reason to make two different publish - if all “SQL” in helpers?

Nope, there’s a difference. You need both publish, but repeat their queries in the helpers.

1 Like