javascript - {{#if currentUser}} 太慢

标签 javascript meteor

我在我的网站上创建了一个管理区域,该区域受登录保护,由帐户包提供支持。 我的管理模板当前如下所示:

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

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

最佳答案

将登录逻辑带到 View 上可能是一种简单的方法,但正如您所看到的,这是不值得的。

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

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

以下是其中一些:

  • meteorhacks:fastRender

  • meteorhacks:订阅管理器

  • kadira:flow-router(而不是 Iron:router,后者在重新运行路线和渲染方面更加随机。)

下面是一些如何使用 Flow Router 处理它的示例。 以下示例架构是根据 Meteor Chef 模型构建的。 在此示例中,我假设您根据最新版本的 Ecmascript 使用 alaning:roles 包和代码。

/both/routes/__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 & FlowRouter文档(作者:Kadira)

关于javascript - {{#if currentUser}} 太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33630552/

相关文章:

javascript - 将所有图像的源放入数组中

javascript - 使用 mousedown 模拟 Spinner 数字

javascript - Jest 报告测试通过,即使 expect 断言失败

javascript - 根据选择框值限制javascript切换

javascript - jQuery:根据元素可见性显示/隐藏警报

javascript - 在 Meteor 的模板渲染函数中访问父数据上下文

image - meteor 图像,CSS, "Normal"Web 服务

javascript - 服务器代码中无法识别 Meteor 模板

node.js - LAN + Internet Meteor 应用程序可以吗?

node.js - 在 Meteor.js 上定义证书颁发机构