Search code examples
javascriptmeteormeteor-blaze

How is it possible to call function from different templates in blazeComponent?


I want to call a method from different template, for example:

class studens extends BlazeComponent {
    average() {}
}
studens.register("templatestudens");

class teacher extends BlazeComponent {
    // how to call templatestudens 'average' function from here?
}
teacher.register("templateteacher");

Solution

  • I would recommend you to define methods in global register helper. Then you can call that method from any template as below;

    CODE for Global declaration for method:

    Template.registerHelper("average", function(){
      //code here
    });
    

    CODE to call the global helper (in js file) method from any template

    Template.YOUR_TEMPLATE.helpers({
      getAverage(){
        return Blaze._globalHelpers['average']();
      }
    });
    

    I hope this helps.