How to suppress the initial load for observe changes on the client side?

How do you suppress the initial load when observing changes on a collection with observeChanges.

I’ve seen this post on stackoverflow but the solution is not working for me. I suspect it’s because I’m running it on the client-side.

Here is my code:

var posts = null;
var init = true;

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

Template.mainChat.onRendered(function() {
  posts.observe({
    added: function() {
      if (!init)
        console.log('foo');
    }
  });

  init = false;
});

I’ve also tried placing init = false after posts = Posts.find() but that didn’t work either. console.log('foo') is still executing for every document found and not only on new additions to the collection.

Thanks


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

Template.mainChat.onRendered(function() {
  var init = true;
  Posts.find().observe({
    added: function() {
      if (!init)
        console.log('foo');
    }
  });

  init = false;
});
2 Likes

This didn’t work, it’s still logging once for every document

Since this is running on the client. If this runs before there is any data then this will execute and set init to false. Then as the data starts coming in, the added callback will be executed for each post.

1 Like

This is the correct read.

If you have access to the subscription object, you can verify its ready before running this logic loop.

2 Likes

Thanks, this is what I ended up doing