简体   繁体   中英

Meteor - How call another function in same template helper

I am trying Meteor. I just want to call another function from one function and it gives me reference error saying xxx not defined.

In my html file:

<template name="hello">
    {{getDaysInMonth}} 
</template>

In js file:

Template.hello.helpers({
  getDaysInMonth: function(){
    var now = new Date();
    return getDaysInParticularMonth(now.getMonth(), now.getFullYear()); // Meteor does not find this function
  },
  getDaysInParticularMonth: function(month, year) {
     console.log("hey"); 
     return 0;     //just for test
  },

});

Output

 ReferenceError: getDaysInParticularMonth is not defined

Plz help. Thanks,

Declare a method outside the template helpers

function commonMethod(month, year) {
    console.log("hey"); 
    return 0;     //just for test
}

Template.hello.helpers({
  getDaysInMonth: function(){
    var now = new Date();
    return commonMethod(now.getMonth(), now.getFullYear()); // Meteor does not find this function
  },
  getDaysInParticularMonth: function(month, year) {
    var now = new Date();
    return commonMethod(now.getMonth(), now.getFullYear());
  },
});

There is a trick that you can use meteor execute the functions call from right to left so your one function output will be because input for the another function and so on. I hope that make sense to you.

Your html code

<template name="hello">
    {{getDaysInParticularMonth getDaysInMonth}} 
</template>

Your js code

Template.hello.helpers({
  getDaysInMonth: function(){
    var now = new Date();
    return [now.getMonth(), now.getFullYear()];
  },
  getDaysInParticularMonth: function(array) {
     console.log("hey"); 
     return 0;     //just for test
  },
});

But if you want to to just call a function from the helper then you have to define the function outside of helper block this is how you can do that as well.

In my html file:

<template name="hello">
    {{getDaysInMonth}} 
</template>

In js file:

Template.hello.helpers({
  getDaysInMonth: function(){
    var now = new Date();
    return getDaysInParticularMonth(now.getMonth(), now.getFullYear());
  },

});

function getDaysInParticularMonth(month, year) {
     console.log("hey"); 
     return 0;     //just for test
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM