0

When doing the following implementation , i get problems because it's look like the data is not available in client side:

In The global configuration of Iron-router , i subscribe to "notifications" publication . Then i fetch the notifications, and i collect the IDs of elements based on type. Then i subscribe to the publications using the collected IDs.

In a template that displays the notifications details, i loop over notifications via Notifications.find() , and then depending on the type of notifications, i do : Questions.findOne(_id) or Answers.findOne(_id) by i am getting undefined exception when i try to access the elements fields.

Router.configure({
      layoutTemplate: 'layout',
      loadingTemplate: 'loading',
      waitOn: function() { 
      Meteor.subscribe('notifications', function() {
      var notifications = Notifications.find().fetch();
        var questionsIds = _.map(_.filter(notifications, function (notif) {
        return notif.targetObjectType == 'QUESTION';
      }), function(q) { return q.targetObjectId});

     var answersIds = _.map(_.filter(notifications, function (notif) {
     return notif.targetObjectType == 'ANSWER';
     }), function(q) { return q.targetObjectId});

     Meteor.subscribe('notificationsAnswers', answersIds);
     Meteor.subscribe('notificationsQuestions', questionsIds);

How is it possible to wait for the "nested" subscriptions to be ready to use them in helper functions ?

2 Answers2

0

Just a thought, have you tried using Deps.autorun ('notificationsAnswers' with a Session variable), and on your function notifications return along with Session.set()?

So you only waitOn Questions, and once find the q.targetObjectId, set the Session of answer will then trigger its subscription according.

Anzel
  • 19,825
  • 5
  • 51
  • 52
0

The solution is to move this code :

var notifications = Notifications.find().fetch();
        var questionsIds = _.map(_.filter(notifications, function (notif) {
        return notif.targetObjectType == 'QUESTION';
      }), function(q) { return q.targetObjectId});

     var answersIds = _.map(_.filter(notifications, function (notif) {
     return notif.targetObjectType == 'ANSWER';
     }), function(q) { return q.targetObjectId});

     Meteor.subscribe('notificationsAnswers', answersIds);
     Meteor.subscribe('notificationsQuestions', questionsIds);

to a onBeforeAction hook in the same global iron-router config like this :

onBeforeAction : function() {

var notifications = Notifications.find().fetch();
        var questionsIds = _.map(_.filter(notifications, function (notif) {
        return notif.targetObjectType == 'QUESTION';
      }), function(q) { return q.targetObjectId});

     var answersIds = _.map(_.filter(notifications, function (notif) {
     return notif.targetObjectType == 'ANSWER';
     }), function(q) { return q.targetObjectId});

     Meteor.subscribe('notificationsAnswers', answersIds);
     Meteor.subscribe('notificationsQuestions', questionsIds);
}