流星 - 在同一个模板助手中如何调用另一个函数

流星 - 在同一个模板助手中如何调用另一个函数

问题描述:

我正在尝试流星。我只想从一个函数调用另一个函数,它给了我参考错误,说xxx未定义。流星 - 在同一个模板助手中如何调用另一个函数

在我的html文件:

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

在js文件:

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 
    }, 

}); 

输出

ReferenceError: getDaysInParticularMonth is not defined 

plz帮助。谢谢,

以外的方法有,你可以使用流星执行功能从右调用左边,这样你的一个函数输出将是一招,因为对于其他功能输入等等。我希望对你有意义。

HTML代码

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

你的js代码

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 
    }, 
}); 

但是,如果你想只是调用一个函数从助手,那么你必须定义帮手块之外的功能,这是你如何做到这一点。

在我的html文件:

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

在js文件:

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 
}, 

声明模板助手

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()); 
    }, 
}); 
+0

有什么理由原代码失败了?任何为什么Template.instance()。methodname()都不起作用? – droidbee