gpt4 book ai didi

javascript - {{#if currentUser}} 太慢了

转载 作者:行者123 更新时间:2023-11-29 18:02:57 26 4
gpt4 key购买 nike

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

<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 处理它的一些示例。以下示例架构是根据 The 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com