Logging template renders

Good day,
I would like to monitor how many times each template is rendered.
I have placed the following code in my isClient conditional:
logRenders=function() {
_.each(Template, function (template, name) {
var oldRender = template.rendered;
var counter = 0;

        template.rendered = function () {
            console.log(name, "render count: ", ++counter);
            oldRender && oldRender.apply(this, arguments);
        };
    });
}

When I execute this function in my browser console (after starting my app) I get “TypeError: template is undefined”.

Any advice will be greatly appreciated.

Not sure exactly why you are getting the error being that template is clearly a parameter of the each callback. That being said, lets see if we can get you a working solution…

var logRenders = function() {
    var renderFunc = function(){
        var counter = 0;
        return function(){
            console.log(this.view.name, "render count: ", ++counter);
        };
    };
    for(var template in Template){
        template = Template[template];
        if(template && template.onRendered){
            template.onRendered(renderFunc());
        }
    }
};

Hi Copley,
Thank you for the reply. This may sound stupid, but how do I call this
function? I see it is defined locally (var). If I remove the var and type
"logRenders()" in my webconsole I get undefined. ie the function returns
nothing. So, how do I run this function?

Best regards

after you call logRenders() from the console you will see it return undefined. Then you need to cause a template to be rendered and you will see the a message logged to the console with the number of times that template has been rendered.

Cool!

You are a legend!

1 Like