Search code examples
javascripthandlebars.jspartials

How to execute a Javascript callback when a partial is rendered in Handlebars


I've already created a custom class in JS that wraps Handlebars, this way I can execute a callback when a template has been rendered, but since partials in Handlebars are automatically generated, how could I execute a callback when a partial is rendered ?

I've tried to override the '>' helper but it doesn't work... and I have no idea how to do it otherwise.

Thank you guys for the help.


Solution

  • Handlebars runtime calls method Handlebars.VM.invokePartial to exec the partial. You can override it for adding your callback function call:

    Handlebars.VM.invokePartialOrigin = Handlebars.VM.invokePartial;
    Handlebars.VM.invokePartial = function () {
      var args = [].slice.call(arguments, 0);
      Handlebars.VM.invokePartialOrigin.apply(Handlebars.VM, args);
      callback(args);
    };