1

I've a publication which sends limited number of records based on start and limit

Metero.publish("posts",function(start,limit){
 return Posts.find({},{"start":start,"limit":limit});
});

I'm subscribing to the publish function in autorun function.

My problem is I already have few records of posts collection in client which is used by another table

the current publish function may have records that already existed in client

I just want to know the records that are sent by the current publish function.

Any hacks or work arounds are also welcome.

EDIT I want to display those records which are published in a table, as I already have some data in client It is tought to filter out what records are sent to the client by the publish function

user555
  • 1,489
  • 2
  • 15
  • 28
  • I suppose you could add a [transform](http://stackoverflow.com/questions/23919845/replace-attributes-of-a-document-in-publish-function) to mark the posts from this publish function. BTW, do you mean `skip` instead of `start`? – David Weldon Jan 02 '15 at 06:15
  • @DavidWeldon, yeah skip – user555 Jan 02 '15 at 06:17
  • possible duplicate of [Meteor publish/subscribe strategies for unique client-side collections](http://stackoverflow.com/questions/12223866/meteor-publish-subscribe-strategies-for-unique-client-side-collections) – malhal Aug 04 '15 at 14:50

3 Answers3

2

Try this publish function,

Meteor.publish('posts', function(limit) {
  if (limit > Posts.find().count()) {
    limit = 0;
  }

  return Posts.find({ },{limit:limit});
});

now on the client.js

Template.Posts.created = function() {
  Session.setDefault('limit', 10);
  Tracker.autorun(function() {

    Meteor.subscribe('getPosts', Session.get('limit'));
  });
}

now you can use this helper,

Template.Posts.helpers({
  posts: function() {
    return Posts.find({ }, { limit: Session.get('limit') });
  }
});

and using it like any normal helper on some each helper

<template name="Posts">
{{#each posts}}
{{namePost}} <!-- or whatever register on the posts mongo document -->
{{/each}}
<!-- button to load more posts -->
<button class="give-me-more">Click for more posts </button>
</template>

now if you want to increment the number of post by 10 x 10 use this function

incrementLimit = function(inc=10) {
  newLimit = Session.get('limit') + inc;
  Session.set('limit', newLimit);
}

and call it on a click event like this

Template.Posts.events({
  'click .give-me-more': function(evt) {
    incrementLimit();
  }
});

Now each time the posts template its created you will get only 10 posts on each template you use this helper, and load 10x each time button its clicked

This is the same code from Gentlenode

i just added the HTML, hope this help you

Ethaan
  • 11,291
  • 5
  • 35
  • 45
0

Have you considered logging it to the console?

Metero.publish("posts",function(start,limit){
 var currentPosts = Posts.find({},{"start":start,"limit":limit});
 console.log(currentPosts);
 return currentPosts;
});
Ethaan
  • 11,291
  • 5
  • 35
  • 45
AJ.
  • 1,248
  • 10
  • 27
  • No, I want to display those records which are published in a table, as I already have some data in client It is tought to filter out what records are sent – user555 Jan 02 '15 at 05:54
-1

I ended up using 'client side collections' with custom publications for different tables

user555
  • 1,489
  • 2
  • 15
  • 28
  • It's done by simply querying the client collection with the same query that was used to create the publication on the server: http://stackoverflow.com/a/13076752/259521 – malhal Aug 04 '15 at 14:50
  • @malhal, no it is not that simple. What if the same record qualifies for both publish functions in client side, In that case record count will be more than what needed right? – user555 Aug 19 '15 at 09:24