问题
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