Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render callback to all templates in meteor blaze

I am forced to assign rendered callbacks to all my templates.

Until 0.9.0 I used to do it like this:

_.each( Template, function( template, name ) {
  //...
  template.rendered = function() {
    //...
  };
});

But now, Template is a constructor and not an object, so this method won't work here. Is there any way to pass callback function to all templates or fire function when all templates were rendered using Blaze?

like image 762
Adam Wolski Avatar asked Oct 09 '14 14:10

Adam Wolski


1 Answers

Here is a quick workaround I came up with, iterating over every Template property to find out if it corresponds to a template definition, and if it does, assign the onRendered callback.

// make sure this code is executed after all your templates have been defined
Meteor.startup(function(){
  for(var property in Template){
    // check if the property is actually a blaze template
    if(Blaze.isTemplate(Template[property])){
      var template=Template[property];
      // assign the template an onRendered callback who simply prints the view name
      template.onRendered(function(){
        console.log(this.view.name);
      });
    }
  }
});

I don't know what's your use case so there may be better solutions depending on it.

like image 65
saimeunt Avatar answered Oct 22 '22 10:10

saimeunt