Meteor waitOn:设置会话值,然后使用定义订阅
这是我的路线:Meteor waitOn:设置会话值,然后使用定义订阅,meteor,Meteor,这是我的路线: this.route('friends', { path: '/friends', waitOn: function() { return Meteor.subscribe('friendsPlaylists', Session.get('friends')) } }); 在此路由之前,我调用一个命中api的函数,并设置会话值: Router.onBeforeAction(getFriends, {only: 'friends'})
this.route('friends', {
path: '/friends',
waitOn: function() {
return Meteor.subscribe('friendsPlaylists', Session.get('friends'))
}
});
在此路由之前,我调用一个命中api的函数,并设置会话值:
Router.onBeforeAction(getFriends, {only: 'friends'})
var getFriends = function() {
Meteor.call('getFriendsData', function(err, result) {
Session.set('friends', result.data);
Session.set('friendsLoaded', true);
});
}
当Meteor.subscribe返回时,由于异步行为,会话未设置。我如何让订阅工作,它将在哪里等待会话设置?提前感谢您的帮助 FYI onBeforeAction有一个名为pause的参数,它是一个暂停路由执行的函数,因此您可以像这样重写代码:
Router.onBeforeAction(getFriends, {only: 'friends'})
var getFriends = function(pause) {
Meteor.call('getFriendsData', function(err, result) {
Session.set('friends', result.data);
Session.set('friendsLoaded', true);
});
if(!Session.get('friendsLoaded')){ pause(); }
else{
this.subscribe('friendsPlaylists', Session.get('friends')).wait();
}
}
请参阅:谢谢,这似乎有效。我得把.wait()这个词去掉。因为它会永远挂着。知道为什么吗?