Search code examples
meteoriron-router

How to do sequential subscription in Meteor(iron:router)


For example, I have to something like this:

Router.route('/threadList', {
waitOn: function () {
    return Meteor.subscribe('threads', Meteor.userId);
},
data: function() {
    threads = Threads.find();
    _.each(threads, function(thread) {
        _.each(thread.comments, function(commentId) {
            Meteor.subscribe('comments', commentId);
       })
   })
}

I dont know how to do this kind of thing in Meteor


Solution

  • you can check out the package publish-composite

    to do reactive joins in your publication code.

    just add it to your project: meteor add reywood:publish-composite

    then do your publish like so:

    Meteor.publishComposite('publishName', { 
        find: function() {
            return Threads.find({userId: this.userId});
        },
        children: [
            {
                find: function(thread) {
                     return Comments.find({threadId: thread._id});
                }
            }
    });