I have tried this a couple different ways and they both behave the same way (see below for code). I'm using a spacebars if condition (and tried using a helper as well) to check if the user is logged in, then display login/sign up links if they aren't. If they are, hide them.
What I noticed is that on the initial page load (if they navigate back from a different site), the login/sign up links show quickly before hiding (if the user is still logged in). Is there a way to insure that no elements render in the view if the condition is false? It seems to me that it should check before the view starts rendering, then display the appropriate elements on the page.
Thanks for the help! -Chris
Solution to the flicker that I was experiencing: I was checking against the user, though the view was rendering quicker than the DB query. I added a guard expression (see below) and that seems to take care of the flicker.
isUserLoggedOut: function() {
var user = Meteor.user();
if(Meteor.user()) {
return user && false;
} else{
return user && true;
}
}
Attempt #1:
Template.headerTpl.helpers({
isUserLoggedIn: function() {
var user = Meteor.user();
if(user) {
return false;
} else{
return true;
}
}
});
<template name="headerTpl">
{{#if isUserLoggedIn}}
<li style="display:none;"><a href="{{pathFor 'userRegistrationFormTpl'}}" id="signup-js">Sign Up</a></li>
<li><a href="{{pathFor 'userLoginFormTpl'}}" id="login-js">Login</a></li>
{{/if}}
</template>
Attempt #2:
Template.headerTpl.helpers({
isUserLoggedIn: function() {
var user = Meteor.user();
if(user) {
return "hide";
} else{
return "show";
}
}
});
<template name="headerTpl">
<li class={{isUserLoggedIn}}><a href="{{pathFor 'userRegistrationFormTpl'}}" id="signup-js">Sign Up</a></li>
<li class={{isUserLoggedIn}}><a href="{{pathFor 'userLoginFormTpl'}}" id="login-js">Login</a></li>
</template>
Attempt #3:
{{#if currentUser}}
{{else}}
<li style="display:none;"><a href="{{pathFor 'userRegistrationFormTpl'}}" id="signup-js">Sign Up</a></li>
<li><a href="{{pathFor 'userLoginFormTpl'}}" id="login-js">Login</a></li>
{{/if}}
Attempt #4:
<template name="headerTpl">
{{#if isUserLoggedOut}}
{{> signInLinksTpl}}
{{/if}}
</template>
<template name="signInLinksTpl">
<li style="display:none;"><a href="{{pathFor 'userRegistrationFormTpl'}}" id="signup-js">Sign Up</a></li>
<li><a href="{{pathFor 'userLoginFormTpl'}}" id="login-js">Login</a></li>
</template>
Template.headerTpl.helpers({
isUserLoggedOut: function() {
if(Meteor.user()) {
return false;
} else{
return true;
}
}
});