1

I have groups and items and the items are related to specific groups. At the "detail page" of a group I want to see all the items belonging to the specific group.

I've tried this

Router.route('/group/:_id', {
  name: 'group',
  template: 'group',
  waitOn: function () {
    return this.subscribe("groups", this.params._id);
  },
  data: function () {
    return {
      group: Groups.findOne(this.params._id);
      items: Items.find({groupId: this.params._id}),
    }
  }
});

But what should waitOn look like if it should both wait for the specific group AND the items belonging to the group?

Jamgreen
  • 10,329
  • 29
  • 113
  • 224

2 Answers2

2

You can return an array of subscriptions to wait on:

waitOn: function () {
    return [
        Meteor.subscribe("groups", this.params._id),
        Meteor.subscribe("items", this.params._id)
    ]
}
Tarang
  • 75,157
  • 39
  • 215
  • 276
  • Yup. I really wish the Iron Router docs would just create an example code section with this because it's not at all obvious from the docs. – fuzzybabybunny Dec 17 '14 at 08:37
0

You can either have another publish function

 Meteor.publish('relatedItems', function (groupId) {
   return Items.find({groupId: groupId});
 });

and wait for the two subscriptions

 waitOn: function () {
   return [
     Meteor.subscribe("groups", this.params._id),
     Meteor.subscribe("relatedItems", this.params._id)
   ];
 },

or you can add to your existing publication like this:

 Meteor.publish('groups', function (groupId) {
   return [
      Groups.find({_id: groupId}),
      Items.find({groupId: groupId}),
   ];
 });
Tomasz Lenarcik
  • 4,762
  • 17
  • 30