Meteor 流星铁路由器走的是一条路线';s waitOn first而不是Router.onBeforeAction

Meteor 流星铁路由器走的是一条路线';s waitOn first而不是Router.onBeforeAction,meteor,iron-router,Meteor,Iron Router,我有Meteor角色包,我正在尝试定义一个管理路径: var requireLogin = function() { if (! Meteor.user()) { debugger // #1 if (Meteor.loggingIn()) { this.render(this.loadingTemplate); } else { console.log("no user"); this.render('AdminLogin');

我有Meteor角色包,我正在尝试定义一个管理路径:

var requireLogin = function() {
  if (! Meteor.user()) {
    debugger // #1
    if (Meteor.loggingIn()) {
      this.render(this.loadingTemplate);
    } else {
      console.log("no user");
      this.render('AdminLogin');
    }
  } else {
    this.next();
  }
};

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

Router.route('/admin', {
  name: 'AdminMain',
  layoutTemplate: 'AdminLayout',
  waitOn: function(){
    debugger // #2
    return [
      Meteor.subscribe("appointments")
    ]  
  }
});
我在服务器/出版物中看到了这一点:

Meteor.publish('appointments', function() {
  if (Roles.userIsInRole(this.userId, ['assistant','admin'])) {
    return Appointments.find();
  } else {
    console.log("no user");
    return [];
  }
});
首先触发的调试器是
waitOn
中的调试器2。为什么?我有一个
OnBeforeAction
,正好为该路线指定。根据Iron路由器指南,当用户导航到“/admin”时,我们的onBeforeAction钩子函数将在路由函数之前运行。如果用户未登录,则永远不会调用route函数,AdminPage也不会呈现到该页面。


当然,考虑到调试器首先停止等待Meteor订阅,route函数似乎是在OnBeforeAction之前被调用的。由于此订阅要求管理员用户登录服务器,因此如果我在调试器上按“继续”,服务器控制台将记录“无用户”,加载屏幕将永远持续。从不调用
requireLogin
的实际
OnBeforeAction
函数。

waitOn在OnBeforeAction之前调用。这种行为是正确的。从iron router文档:

另一种选择是使用waitOn而不是subscribe。这具有相同的效果,但会自动短路路由操作和任何before挂钩(请参见下文),并呈现loadingTemplate

要处理订阅,您可以使用“订阅”选项:

Router.route('/post/:_id', {
  subscriptions: function() {
    // returning a subscription handle or an array of subscription handles
    // adds them to the wait list.
    return Meteor.subscribe('item', this.params._id);
  },

  action: function () {
    if (this.ready()) {
      this.render();
    } else {
      this.render('Loading');
    }
  }
});

请参阅: