How to use wait () in OnBeforeAction

I see old posts where older versions of Iron-router are waiting for () before:

before: function() {
      // let make sure that the topPosts subscription is ready and the posts are loaded
      if (this.data()) {
        // we can then extract the userIds of the authors
        var userIds = this.data().map(function(p) { return p.userId });
        // and add the authors subscription to the route waiting list as well
        this.subscribe('authors', userIds).wait(); **<--- this guy!**
      }
    }

      

Above from https://www.discovermeteor.com/blog/reactive-joins-in-meteor/

If I add wait () to my OnBeforeAction subscription, I get the following errors:

You called wait() after calling ready() inside the same computation tree.

You can fix this problem in two possible ways:

1) Put all of your wait() calls before any ready() calls.
2) Put your ready() call in its own computation with Deps.autorun.

      

My waitOn

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

      

and OnBeforeAction

onBeforeAction: function() {
            if (this.ready()) {
                // we can now get the latest (first in the list) week
                var week = SheetData.find().fetch()[0].week;
                this.subscribe('standings', this.params.league, week).wait();
                this.next();

            }
        }

      

If I remove wait (), the render pattern starts before my subscription is ready. The suggested fixes do not appear to be applicable. What am I missing?

+3


source to share


1 answer


Here's a radzserg based solution answers a similar question. It cleverly uses the callback function in the original function waitOn

instead of onBeforeAction

. In your case, it would be:



waitOn: function() {
    var that = this;
    return Meteor.subscribe('weeks', this.params.league, function() {
        var week = SheetData.find().fetch()[0].week;
        that.wait(Meteor.subscribe('standings', that.params.league, week));
    });
}

      

0


source







All Articles