Alert/popup when a collection changes

I am quite certain that my problem has a very simple solution, but I don’t have it.
What I want is simple: I want a popup each time an item is added to collection. Is there and event on the subscription or something i can use? If I use normal templates there’s no problem, but I want to get signalled about the change, examine it and (maybe) act on it

Something like .observeChanges might be what ur looking for:


Thanks, that was exactly what I was looking for!

…and, since I started asking - how do I tell when a subscription is ready?
‘ready’ does not throw errors, but it doesnothing else either…

var sub = Meteor.subscribe("myStuff);
console.log(“sub ready, do stuff”)

If I query MyStuff.find(), MyStuff is undefined unless I artificially delay with setTimeout

EDIT: I’ve got it, just a normal callback:
Meteor.subscribe(“myStuff”, function(){
console.log(“sub ready, do stuff”)

Im not 100% on this but i dont think var sub = Meteor.subscribe("myStuff); is a valid statement…usually its just Meteor.subscribe('nameOfCollection') and then you use your Collection normally. Ill let someone else chime in on that though…

Yes, I just put the solution in my EDIT above

Also, depending on where you are doing your subscription you can handle all of that stuff in the templatename.js file.

  var self = this;
      // do stuff

Or even simpler: {
  var self = this;

  self.autorun(function() {
    self.subscribe('bar', function() {
      // we're ready!

Comment: autorun is not needed if you just want to subscribe once when the template is created.

1 Like

Does this detect if the sub is ready?

Yes, it does. The second argument to template.subscribe is a callback that fires off when the sub is ready. The old syntax used to be:

template.subscribe(publication, {
  onReady: function() { /* ready */ }

Now you can skip onReady and the callback you supply is fired when the sub is ready.


Wow! TIL - That’s great information - Thanks!

I guess the autorun function is more useful if you have multiple subscriptions on a template and you need all of them to do something.

template.autorun is when the contents of the autorun contain reactive data that changes, and you need to be able to re-subscribe to something else if that data changes.

Here’s an example… not a very good one, but just to illustrate:{
  var template = this;

  template.autorun(function() {
    template.subscribe('someSubscription', Session.get('limit'), function() {
      /* Whenever the 'limit' session var changes, this subscribe() call is invoked again. Without autorun(), it would only subscribe once and changes to 'limit' would have  no effect */

I’m about 80% sure I have this right. :wink: Maybe someone else can check what I’ve written, like @seeekr or @serkandurusoy

1 Like

Looks good to me! I haven’t yet had the need to use the callback to the subscribe() method, but the docs say you’re right and as far as I can tell it does what you say it does – namely reactively resubscribe whenever a reactive var within the template.autorun() method changes, and then executes some more code when the subscription is ready!

True, you don’t really need a callback. In your template, you could do:

{{#if Template.subscriptionsReady}}
  show data and stuff
  <!-- show a spinner or do something else -->
  {{> spinner}}