how to know which docs are sent to the client in meteor

岁酱吖の 提交于 2019-12-11 07:12:20

问题


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


回答1:


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




回答2:


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;
});



回答3:


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



来源:https://stackoverflow.com/questions/27737391/how-to-know-which-docs-are-sent-to-the-client-in-meteor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!