流星 - 铁路由器不等待onBeforeAction钩子中的订阅数据



我想在请求的艺术家不可用时设置一个Session变量(artistNotAvailable)。不幸的是,路由不起作用,因为它没有在我的onBeforeAction钩子中等待订阅数据。 artistAvailartist是未定义的,但不是因为数据库中没有艺术家,我认为这是一个订阅问题。我对其进行了调试,并在浏览器控制台中调用Artists.find().fetch();返回undefined

这是我的路线:

this.route('eventPage', {
        path: '/event/:slug/:openDate?/:closeDate?',
        onBeforeAction: [teamFilter, function() {
            var event = this.data();
            if (event.outdoor) {
                this.subscribe('artists', this.params.slug);
                if (this.ready()) {
                    var artistAvail = Artists.findOne({eventId: event._id, openDate: moment(this.params.openDate).toDate(), closeDate: moment(this.params.closeDate).toDate()});
                    if ((!this.params.openDate && !this.params.closeDate) || typeof artistAvail === "undefined") {
                        var artist = Artists.findOne({
                            eventId: event._id,
                            openDate: {$lte: new Date()},
                            closeDate: {$gte: new Date()}
                        });
                        if (Artists.find().count() > 0 && artist) {
                            Session.set('artistNotAvailable', true);
                            this.redirect('eventPage', {
                                slug: this.params.slug,
                                openDate: moment(artist.openDate).format('YYYY-MM-DD'),
                                closeDate: moment(artist.closeDate).format('YYYY-MM-DD')
                            });
                        } else {
                            Session.set('noArtists', true);
                            this.redirect('overviewPage', {slug: this.params.slug});
                        }
                    } else {
                        this.subscribe('musicOutdoor', this.params.slug, moment(this.params.openDate).toDate(), moment(this.params.closeDate).toDate());
                        if (this.ready()) {
                            var guestsIds = new Array();
                            Guests.find({artistId: artistAvail._id}).forEach(function (guest) {
                                guestIds.push(guest._id);
                            });
                            this.subscribe('guestsOutdoor', guestIds);
                        } else {
                            this.render('loading');
                        }
                    }
                } else {
                    this.render('loading');
                }
            } else {
                this.subscribe('musicIndoor', this.params.slug);
                this.subscribe('guestsIndoor', this.params.slug);
                if (!this.ready()) {
                    this.render('loading');
                }
                this.next();
            }
            this.next();
        }],
        waitOn: function() { return [Meteor.subscribe('singleEvent', this.params.slug)]; },
        data: function() {
            return Events.findOne({slug: this.params.slug});
        }
    });

订阅:

Meteor.publish('artists', function(slug) {
    var event = Events.findOne({slug: slug});
    if (event) {
        return Artists.find({eventId: event._id});
    } else {
        return null;
    }
}); 

现在的问题是,当我使用错误的openDatecloseDate访问eventPage时,即使数据库中有艺术家,我也会被重定向到overviewPage。如前所述,我对其进行了调试,似乎this.ready()不起作用。我真的很想念.wait():-(

任何帮助将不胜感激。

Iron Router 工作正常。

this.subscribe就像Meteor.subscribe,Iron-Router不会等待这个。

waitOn 是一个函数,它将等到每个返回的订阅都准备就绪,这就是为什么 this.ready()onBeforeAction 中为真

有几种方法可以做你想做的事

  • 你可以像CodeChimp所说的那样,使用具有创建/销毁的模板来做到这一点
  • 您可以将路线拆分为不同的路线,以便在需要时重定向
  • 你可以在这里使用来自铁路由器新API的.wait()。

它帮助我在数据函数中等待订阅(直到订阅真正准备就绪),如下所示:

Router.route('/:_id', {
    name: 'show',
    template: 'show',
    waitOn: function () {
        var id = this.params._id;
        return Meteor.subscribe('something', id, {});
    },
    data: function () {
        if (this.ready()) {
            var id = this.params._id;
            var data = Somethings.findOne({_id: id});
            return data;
        }
    }
});

最新更新