0
votes

Im new to Meteor and Im trying to figure out how to run a function after a collection change.

I have a route(iron router) that subscribes to a collection with waitOn. Which just waits for the subscrition to be ready before rendering which is what I want.

    waitOn: function () {
        return Meteor.subscribe('collection', this.params._id);
    },

Any changes to the collection will be updated on all the clients and rendered automatically.

How would I run a function once the collection has changed?

1
You can call observe changes or observe depending on if you need the whole document on each change. Normally, helpers are used to change the content of the page instead of calling observe directly. I'd recommend explaining in more detail what you wish to accomplish. - David Weldon
Alright I've tried using a helper it works fine using Meteor.Defer to make changes. I've also tried using onData option on iron router as suggested from the first answer. They both work fine. But which one should I use? and which one works best? - Andre Escudero

1 Answers

1
votes

You can use the onData hook, provided that you're returning that data using the data helper. E.g this is what a route may look like

this.route('routename',
    path : '/abc',
    waitOn: function () {
        return Meteor.subscribe('collection', this.params._id);
    },
    data: function() {
        return Collection.findOne({_id: this.params.id});
    }
    onData: function() {
         //Do something when the data found by the above changes
    }
});