{{#if currentUser}}太慢了

时间:2015-11-10 12:56:32

标签: javascript meteor

我在我的网站上创建了一个管理区域,该区域受登录保护,该区域由帐户包提供支持。 我的管理模板目前看起来像这样:

<template name = "Admin">
{{#if currentUser}}
    {{> SideNav}}
    {{> Template.dynamic template = getCurrent}}
{{else}}
    {{> Login}}
{{/if}}
</template>

它可以工作,但是当我更改网站时,它会在更改为动态模板之前显示登录页面一秒钟。它很短,但你可以注意到它,它看起来非常好看。那我该怎么办呢?我不知道如何解决这个问题。

2 个答案:

答案 0 :(得分:3)

在视图上显示您的登录逻辑可能是一种简单的方法,但正如您所看到的,它是不值得的。

必须在应用程序中尽快完成与登录相关的测试。您应该在路由器中执行此操作,因为它将允许您在视图开始渲染之前有效地管理访问并开始订阅(这最后一点取决于您的包以及您管理渲染的方式)。

此外,在这种情况下,一些软件包提供了非常相关的工具来改善您的应用程序性能和渲染。

以下是其中一些:

  • meteorhacks:fastRender

  • meteorhacks:订阅管理器

  • kadira:flow-router(而不是Iron:路由器,在重新运行路线和渲染时更随机。)

以下是使用Flow Router处理它的一些示例。 以下示例体系结构是根据Meteor Chef模型构建的。 在这个例子中,我假设您根据最新版本的Ecmascript使用alaning:roles包和代码。

<强> /两者/路由/ __ triggers.js

// Let's declare some namespace for our routing triggers
Triggers = {};
Triggers.mustBe = {};
Triggers.mustNotBe = {};

// Here, we check for the state of the client
Triggers.mustBe.loggedIn = ( ) => {
  if (!(Meteor.loggingIn() || Meteor.userId()))
  // If he is not logged in or logging in, we handle the redirection
  {
    FlowRoute = FlowRouter.current();
    if (FlowRoute.route.name != "home")
      Session.set("redirectAfterLogin", FlowRoute.path);
    FlowRouter.go('/splash');
  }
};

Triggers.mustBe.admin = ( ) => {
  // Same here. If the user is not an admin, we should redirect him somewhere or prevent the routing to be executed
  if (!Roles.userIsInRole(Meteor.user(), ['admin']))
    FlowRouter.go(FlowRouter.current().path);
};

// Just an example of what if would looks like if we wanted to be sure the client is not connected (for accessing the login screen for example)
Triggers.mustNotBe.loggedIn = ( ) => {
  if (Meteor.loggingIn() || Meteor.userId())
    FlowRouter.go('/');
};

<强> /both/routes/_configuration.js

// We set the rendering root to the 'body' tag. Check for the doc links I give below
if (Meteor.isClient) Meteor.startup(function() { BlazeLayout.setRoot('body'); });

exposed_Routes = FlowRouter.group({
  name: "exposed",
  triggersEnter: []
});

loggedIn_Routes = FlowRouter.group({
  name: "loggedIn",
  triggersEnter: [
    Triggers.mustBe.loggedIn
  ]
});

// You might see that we declare the admin routes group from the logged_in group. Doing so, we will execute all the logged_in triggers before executing the one we define here. It will allow us to check if the user is connected before checking he is an admin
admin_Routes = loggedIn_Routes.group({
  name: "admin",
  triggersEnter: [
    Triggers.mustBe.admin
  ]
});

<强> /both/routes/admin.js

admin_Routes.route('/admin/reports', {
  name: "reports",
  action: function (params, queryParams) {
    // We use kadira:BlazeLayout package to manage the rendering
    BlazeLayout.render('adminLayout', { main: "someTemplate", menu: "SideNav" });
    // Any other logic you would execute each time you create this route.
  }
});

<强> /client/templates/layouts/admin.html

<template name="adminLayout">
  {{> Template.dynamic template=menu }}
  {{> Template.dynamic template=main }}
</template>

BlazeLayout&amp; FlowRouter文档(由Kadira提供)

答案 1 :(得分:1)

当登录方法正在进行中时,你可能需要{{loggingIn}}帮助,这是一个被动的,并且是true

http://docs.meteor.com/#/full/template_loggingin

<template name = "Admin">
    {{#if loggingIn}}
        Loading...
    {{else}}
        {{#if currentUser}}
            {{> SideNav}}
            {{> Template.dynamic template = getCurrent}}
        {{else}}
            {{> Login}}
        {{/if}}
    {{/if}}
</template>