【问题标题】:Meteor Iron Router goes to a route's waitOn first instead of Router.onBeforeActionMeteor Iron Router 首先进入路由的 waitOn 而不是 Router.onBeforeAction
【发布时间】:2015-02-21 17:10:12
【问题描述】:

我有 Meteor Roles 包,我正在尝试定义一个管理路由:

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 Router 指南,当用户导航到“/admin”时,我们的 onBeforeAction 钩子函数将在我们的路由函数之前运行。如果用户没有登录,路由函数永远不会被调用,AdminPage 也不会渲染到页面。

好吧,考虑到调试器首先停止等待 Meteor 订阅,看起来好像在 OnBeforeAction 之前调用了路由函数。由于此订阅需要管理员用户登录服务器,如果我在调试器上按继续,服务器控制台会记录“无用户”,并且加载屏幕会永远持续下去。 requireLogin 的实际 OnBeforeAction 函数永远不会被调用。

【问题讨论】:

标签: meteor iron-router


【解决方案1】:

waitOn 在 OnBeforeAction 之前调用。这种行为是正确的。来自 Iron-router 文档:

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

Source

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

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');
    }
  }
});

Source

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多