流星需要时间来了解是否有{{currentUser}} [英] Meteor takes time to know if there's a {{currentUser}} or not
问题描述
我有一些代码只在有noUser
时才运行,而有一些代码只有在currentUser
时才运行.
所有这些都在导航模板中.像这样...
I've a few code that I want to run only when there's noUser
and a few when there's a currentUser
.
All these are inside the navigation template. Like so...
{{#if currentUser}}
<li class="nav"><a href="{{pathFor 'create'}}">Post</a>
</li>
<li class="nav"><a>Ola, {{thisUser}}!</a>
</li>
<li class="nav"><a href="#" id="logout">Log Out</a>
</li>
{{/if}}
{{#if noUser}}
<li class="nav"><a href="{{pathFor 'signup'}}">Sign Up</a>
</li>
<li class="nav"><a href="{{pathFor 'login'}}">Login</a>
</li>
{{/if}}
所以问题是当出现currentUser
(即,我已登录)并且刷新页面时,首先显示{{#if noUser}}
块中的代码,然后显示{{#if currentUser}}
块而{{#if noUser}}
块只能在没有用户的情况下显示.
这是模板的帮助程序代码.
So the problem is that when there's a currentUser
(i.e, I'm logged in) and I refresh the page, the code inside the {{#if noUser}}
block shows up first then the {{#if currentUser}}
block, while the {{#if noUser}}
block was only meant to show up when there is no user.
Here's the helper code for the template..
Template.navigation.helpers({
thisUser: function () {
return Meteor.user().username;
},
noUser: function () {
var user = Meteor.user();
if (!user) {
return true;
};
}
});
不知道我在做什么错. :(
请帮忙.
Don't know what am I doing wrong here. :(
Please help.
推荐答案
您应使用if else条件代替noUser helper.为了防止在登录时显示"noUser"块,您必须使用 {{loggingIn}}帮助器一个>.像这样:
You should use if else conditions instead of noUser helper. And to prevent showing "noUser" block while logging in you have to use {{ loggingIn }} helper. Something like this:
{{#if loggingIn}}
<p>Loggin in...</p>
{{else}}
{{#if currentUser}}
<li class="nav"><a href="{{pathFor 'create'}}">Post</a>
</li>
<li class="nav"><a>Ola, {{thisUser}}!</a>
</li>
<li class="nav"><a href="#" id="logout">Log Out</a>
</li>
{{else}}
<li class="nav"><a href="{{pathFor 'signup'}}">Sign Up</a>
</li>
<li class="nav"><a href="{{pathFor 'login'}}">Login</a>
</li>
{{/if}}
{{/if}}
因为Meteor不能立即知道用户是否已登录.因此,您必须使用loggingIn助手.
Because Meteor does not know immediately whether user is logged in or not. Therefore you have to use loggingIn helper.
这篇关于流星需要时间来了解是否有{{currentUser}}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!