I have a template rendered in the HTML file with:
{{> header}}
This template renders a Materialize CSS sidebar:
{{#if currentUser}}
<a href="#" data-activates="slide-out" class="expand-navigation left"><i class="mdi-navigation-menu"></ii></a>
<ul id="slide-out" class="side-nav">
{{#each modules}}
{{> module}}
{{/each}}
</ul>
{{/if}}
<template name="module">
<li><a href="#">{{name}}</a></li>
</template>
I have tried the following places to init the plugin:
- Template.header.onRendered(function () {});
- Tracker.autorun(function(){ if(Meteor.userId()){}});
- Template.header.rendered = function () {}
- Template.header.rendered = function () { this.autorun(_.bind(function() { Tracker.afterFlush(function(){});}, this));};
The problem seems to be related to the if statement. If I remove the “{{#if currentUser}}”, it works fine. How do I catch the event that the user is logged in and the template is done rendering?
Sometimes (very seldom) it works. So is that a race condition? How can I influence it?
Edit 1:
If I call the init code on Template.module.onRendered it works, but is executed x times. There must be a better solution…