【问题标题】:waitOn: setting a session value and then using to define a subscriptionwaitOn:设置会话值,然后使用定义订阅
【发布时间】:2014-08-13 21:12:28
【问题描述】:

这是我的路线:

  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 时,由于异步行为,会话未设置。如何让订阅工作,它将在哪里等待会话设置?提前感谢您的任何帮助!

【问题讨论】:

    标签: meteor


    【解决方案1】:

    仅供参考 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();
      }
    }
    

    见:https://github.com/EventedMind/iron-router/blob/devel/DOCS.md#using-hooks

    【讨论】:

    • 谢谢,这似乎有效。我不得不删除 .wait() 。因为它会永远挂起。知道为什么吗?
    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 2015-07-07
    • 2020-03-26
    • 2014-03-18
    相关资源
    最近更新 更多