Use .onRendered for global?


#1

I create the Blaze Layout

<template name="appLayout">
  {{> Template.dynamic template=main }}
</template>

Could I use template level

Template.main.onRendered();
......................onCreated();
...........

#2

Maybe I do not quite understand your question, but in the above example, you also will have

<template name="main">
  <!-- content -->
</template>

And of course, that may use its onRendered() etc.


#3

I use FlowRouter, BlazeLayout and fezvrasta:bootstrap-material-design.
I would like to init the $.material.init(); for all of content template rendered.
But I do this many time on the all content
Ex:

<template name="home">
</template>

Template.home.onRendered(...$.material.init();...);
-------------------------
<template name="about">
</template>

Template.about.onRendered(...$.material.init();...);
-------------------

So I want to config this for all.
Please help me.


#4

So you are looking for ONE global Template.all.onRendered() hook which is done everytime a template gets created, rendered, … ?

P.S.: In that case you should change your title and question and use your second comment instead


#5

Thanks for your advice, please example for me.


#6

Google is your friend if look for the right question:

see: http://stackoverflow.com/questions/26281201/render-callback-to-all-templates-in-meteor-blaze


#7

Look great, thanks again.
Excuse me, this solution make the project slow or not?


#8

Why should it?

It just inserts an additional onRendered function to your templates.

Same as you can do like

Template.main.onRendered(function () {
  // do this
});

Template.main.onRendered(function () {
  // do that
});

A template may gets registered more than one function.

The Meteor.startup() will just insert that function once on startup.


Update: At least any line of code will slow down your app. Best app in case of performance is:

function main() {}

:wink:


#9

Thanks again for your helping.