如何在OnBeforeAction中使用wait()

时间:2014-11-08 21:28:01

标签: iron-router

我看到旧版本的Iron-router在之前等待()的旧帖子:

before: function() {
      // let's 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's waiting list as well
        this.subscribe('authors', userIds).wait(); **<--- this guy!**
      }
    }

以上来自https://www.discovermeteor.com/blog/reactive-joins-in-meteor/

如果我将wait()添加到我的OnBeforeAction订阅中,我会收到以下错误:

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.

我的waitOn是

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

和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();

            }
        }

如果我删除了wait(),则渲染模板会在我的订阅准备就绪之前启动。建议的修复程序似乎不适用。我错过了什么?

1 个答案:

答案 0 :(得分:0)

以下是针对类似问题的基于radzserg's answer的解决方案。他巧妙地在原始waitOn函数中使用了回调函数,而没有使用onBeforeAction。在你的情况下,它将是:

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));
    });
}