waitOn 、 subscriptions 和 onBeforeAction 下的订阅集合有什么区别

what is the difference of subscribing collection under waitOn , subscriptions and onBeforeAction

我想知道以下订阅数据的方式是否有区别,

使用 waitOn

waitOn:function(){
  Meteor.subscribe('//some published function)
 }

使用 onBeforeAction

Router.onBeforeAction : function(){
   Meteor.subscribe('//some published function)
 }

使用订阅

subscriptions: function() {
    this.subscribe('items');
   }

如果您只想为授权用户发布数据,您可以在 onBeforeAction 中检查(如果用户已通过身份验证)路由。类似于:

Router.map(function(){

  this.route('home', {
    path : '/',
    template : 'home'
  });

  this.route('register', {
    path : '/register',
    template : 'register'
  });

  this.route('login', {
    path : '/login',
    template : 'login'
  });

  this.route('requestlisting', {
    path : '/requestlisting',
    template : 'requestlisting',
    waitOn : function(){
        return Meteor.subscribe('RequestsPublication');
    }
  });
  ...
});

var requireLogin = function () {
   if(!Meteor.user()){
       if(Meteor.loggingIn()){
          this.render(this.loadingTemplate);
       }else{
          Router.go('login');
       }
   } else {
       this.next();
   }
}

Router.onBeforeAction(requireLogin, {only: ['requestlisting',...]});

在此示例中,在 onBeforeAction 中,仅当用户登录时 'requestlisting' 才会发生路由,之后订阅任何数据才有意义。