Wait for parent's subscription in child


#1

I use the angular ui-router to create a structur like this:

      .state('home.dashboard', {
    url: '/dashboard',
    templateUrl: 'client/templates/home/dashboard/index.ng.html',
    controller: 'DashboardCtrl',
    abstract: true,
    resolve: {
      "currentUser": ["$meteor", function ($meteor) {
        return $meteor.requireValidUser(function () {
          if (Roles.userIsInRole(Meteor.userId(), ['dashboard'])) {
            return true;
          }

          return 'UNAUTHORIZED';
        });
      }]
    }
  })
      .state('home.dashboard.edit', {
    url: '/edit',
    templateUrl: 'client/templates/home/dashboard/edit/index.ng.html',
    abstract: true
  })
      .state('home.dashboard.edit.template', {
    url: '/template/{templateId:[a-zA-Z0-9]{1,}}',
    views: {
      '': {
        templateUrl: 'client/templates/home/dashboard/edit/template.ng.html',
        controller: 'DashboardEditTemplateCtrl'
      },
      'form_template@home.dashboard.edit.template': {
        templateUrl: 'client/templates/home/dashboard/form/form_template.ng.html'
      }
    }
  })

In ‘home.dashboard’ I subscribe to every collection in need in my child’s logic.

      $meteor.autorun($scope, function () {

    $meteor.subscribe('ownTemplates').then(function () {
      $scope.templates = $meteor.collection(function () {
        return Templates.find({}, {sort: {name: 1}});
      });
    });
  });...

And in ‘home.dashboard.edit.template’ I’ll grab the current template through its ID

$scope.currentTemplate = Templates.findOne({_id: $stateParams.templateId});

This works if I navigate from dashboard -> edit -> template, but reloading the ‘home.dashboard.edit.template’ gives me an undefined $scope.currentTemplate.

I think it’s because the subscription is not done when I call the findOne function. How can I wait for the parent’s subscription to end and then start the finOne function in the child node?


#2

I moved the findOne call into a method and used the promisify package to get a promise when ready. Seems to work stable for now. If somebody got a better solution please feel free.

      var call = Promise.promisify(Meteor.call, Meteor);

  call('getTemplateById', $stateParams.templateId).then(function (value) {
    $scope.currentTemplate = value;
  }).catch(function (err) {
    console.log("Got Error", err);
  });