Alert/popup when a collection changes


#1

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


#2

Something like .observeChanges might be what ur looking for: http://docs.meteor.com/#/full/observe_changes


#3

Thanks, that was exactly what I was looking for!


#4

…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);
sub.ready(function(){
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”)
})


#5

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…


#6

Yes, I just put the solution in my EDIT above


#7

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

Example:

Template.foo.onCreated(function(){
  var self = this;
  self.autorun(function(){
  self.subscribe("bar");
    if(Template.instance().subscriptionsReady()){
      // do stuff
    }
  });
});

#8

Or even simpler:

Template.foo.onCreated(function() {
  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.


#9

Does this detect if the sub is ready?


#10

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.


#11

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.


#12

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:

Template.foo.onCreated({
  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


#13

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!


#14

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

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