Meteor nested template bug


Finding a way to initialize a jquery component, on ALL nested templates, is surprisingly tricky. Outside Meteor this would otherwise be a simple $( document ).ready().

See DEMO of the problem here

See the code here:

One would expect layout.rendered to initialize everything in the sub/nested templates:


 <template name="layout"> 
   {{> ideal}}

 <template name="ideal"> 
  <div class="component"></div>


 Template.layout.rendered = function(){ 
//or alternatively Template.body.rendered()

The component in {{> ideal}} will not initialize if you navigate to it with Iron Router, for example. It only initializes on hard page refresh (because template layout is rendered).

So instead you would have to initialize on each template that element will be used

Template.ideal.rendered ...

Template.x.rendered ...

Template.y.rendered ...

Another solution is putting a 0ms timer on Routes.onAfterAction, but that seems inelegant.

Router.onAfterAction(function() {
  setTimeout(function() {
    $('.layout-yield ul.tabs').tabs();
  }, 0);

Am I missing something in the Docs? Or is this a bug / edge case? IMHO this should be straightforward.


To add to this. We’ve tried iron-router events:


All of these seem to happen before the route’s template content is present. I noticed that onBeforeAction required a call to next() to go on, I even tried looking for the DOM content after the next call.

I also tried rewriting our routes like this:

Router.route('someRoute', function() {
  // look for DOM content, still not found


Update: Meteor.defer() has been suggested but it isn’t any different than setTimeout, as far I can see.


It’s very hacky, butt ugly, and not sure if it works for you and Materialize CSS, but what worked for me a couple times messing with Isotope is just putting it in the template:

<template name="someTemplate">
    {{#if Template.subscriptionsReady}}
        the html you want to use and have the script work on
            the jquery init function


Thanks @camiel

Would like @debergalis opinion here.

Seem’s a bit strange that we would have to resort to hacks like this. Especially for something as basic as $( document ).ready()

The more time that passes without an obvious answer, the more I think this is an omission by Iron Router, or Meteor. Any other solutions out there?


Bumping this. No answer on Stackoverflow either.


Just ran into this issue myself. I hope there is a non-hacky answer out there. Seems like it should be a simple thing. I hope this issue does not turn into a deal-breaker for materialize CSS.


Just to document further:
Some people suggested UI.body.rendered()
Though this will fail. Even with a timeout.
Still perplexed there is not way to do the equivalent of: on document ready.


I just got materialize CSS’s dropdowns working with this package - aldeed:template-extension and this code:

Template.onRendered(function () {

// Initialize all datepicker inputs whenever any template is rendered

Might be the ticket


This is what we ended up doing, but I consider it a hack. Looking for a
better way.

Router.onAfterAction(function() {
setTimeout(function() {
//whatever needs every template and nested template loaded.
}, 0);

Lucas Geiger


@stewartellis see also this pos I just made:

Lucas Geiger