2016-04-29 67 views
0

我正在嘗試流星。我只想從一個函數調用另一個函數,它給了我參考錯誤,說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幫助。謝謝,

回答

1

以外的方法有,你可以使用流星執行功能從右調用左邊,這樣你的一個函數輸出將是一招,因爲對於其他功能輸入等等。我希望對你有意義。

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

聲明模板助手

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

相關問題