0

我正在将 Meteor 与 FlowRouter 一起使用,但我遇到了一个奇怪的行为。我想知道我是否只是从根本上不理解某些东西。我有以下代码client/route.js

"use strict";

FlowRouter.route('/', {
    name: 'home',
    action: function() {
        BlazeLayout.render('main', {main: "homePage"});
    }
});

FlowRouter.route('/admin', {
    name: 'admin',
    triggersEnter: [isUserLoggedIn],
    action: function() {
        BlazeLayout.render('main', {main: "admin"});
    }
});

function isUserLoggedIn() {
    console.log("Worked");
    if (Meteor.userId()) {
        route = FlowRouter.current();
    } else {
        FlowRouter.go("home");
    }
}

我跑meteor了,然后去localhost:3000查看控制台,我看到“Worked”,这意味着 isUserLoggedIn 函数已被触发。我没有点击 admin 或去localhost:3000/admin. 只到顶层。为什么我没有去/admin路由时会触发isUserLoggedIn函数?

编辑 1:看起来我的简化示例实际上工作得很好。实际的问题其实有点像这样:

"use strict";

FlowRouter.route('/', {
    name: 'home',
    action: function() {
        BlazeLayout.render('main', {main: "homePage"});
    }
});

FlowRouter.route('/admin', {
    name: 'admin',
    triggersEnter: [isUserLoggedIn(role)],
    action: function() {
        BlazeLayout.render('main', {main: "admin"});
    }
});

function isUserLoggedIn(role) {
    console.log("Worked");
    if (Meteor.userId() && Role.userIsInRole(role)) {
        route = FlowRouter.current();
    } else {
        FlowRouter.go("home");
    }
}

通过 triggersEnter 传递参数似乎是不可能的(或者我不知道如何使其正常工作)。有没有办法通过 triggersEnter 发送参数?

4

1 回答 1

2

您需要从函数中返回一个函数isUserLoggedIn,如下所示:

function isUserLoggedIn (role) {
  return function (context, redirect, stop) {
    if (Meteor.userId() && Roles.userIsInRole(Meteor.userId(), role)) {
      route = FlowRouter.current();
    } else {
      FlowRouter.go("home");
    }
  }
}
于 2016-01-11T20:54:16.340 回答