struggling to wait on subscriptions and multiple subscriptions

吃可爱长大的小学妹 提交于 2019-12-09 23:26:08

问题


i am really struggling with the iron-router waitOn.

I have a number of subscriptions that i would like to waitOn, it doenst seem to me that iron router is waiting on anything.

I have setup a number of subscriptions (main.js), which i would like to load before the application starts:

Meteor.subscribe("appointments");
Meteor.subscribe("business");
Meteor.subscribe("clients");
Meteor.subscribe("staff"); 

I have tried almost every configuration i possibly can but i cannot seem to get the "loader" to show itself and wait till all the subscriptions are ready. I have a helper function in the layoutTemplate ('layout') to get a value from the database however findOne() returns undefined/null i assume this is because the router has not waited on the subscriptions...

My router configuration. I would like to understand how i can chain the dependencies or create the dependencies to wait.

Router.configure({
    layoutTemplate: 'layout',

    loadingTemplate: 'loading',
    waitOn: function () {
        console.log('Iron router start');
        this.subscribe('clients').wait();
        this.subscribe('staff').wait();
        this.subscribe('appointments').wait();
        this.subscribe('business').wait();
        this.subscribe('calendar').wait();


    },
    action: function() {
        if (this.ready()) {
            this.render('dashboard');
        } else {
            this.render('loading');
        }
    }
});

回答1:


Try changing the waitOn section like this. Also, I think you can remove the subscriptions from the main.js file and write the required/dependent subscriptions in the router's waitOn function for that particular route.

Router.configure({
    layoutTemplate: 'layout',
    loadingTemplate: 'loading',
    waitOn: function () {
        console.log('Iron router start');
        return [        
            Meteor.subscribe('clients'),
            Meteor.subscribe('staff'),
            Meteor.subscribe('appointments'),
            Meteor.subscribe('business'),
            Meteor.subscribe('calendar')
        ]

    },
    action: function() {
        if (this.ready()) {
            this.render('dashboard');
        } else {
            this.render('loading');
        }
    }
});



回答2:


Try moving your subscriptions from onWait into onBeforeAction:

This may work, but i'm not sure. Usually you define the wait params per route not as a global.

Router.configure({
    layoutTemplate: 'layout',
    loadingTemplate: 'loading',
    onBeforeAction: function () {
        console.log('Iron router start');
        this.subscribe('clients').wait();
        this.subscribe('staff').wait();
        this.subscribe('appointments').wait();
        this.subscribe('business').wait();
        this.subscribe('calendar').wait();


    }
});

Router.onBeforeAction('loading');

If this gives you trouble, try using the onBeforeAction above for each of your routes.



来源:https://stackoverflow.com/questions/23977367/struggling-to-wait-on-subscriptions-and-multiple-subscriptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!