2016-04-29 25 views
0

私は流星を試しています。私はちょうど1つの関数から別の関数を呼び出すと、それは私にxxxが定義されていないと言って参照エラーを与えます。 JSファイルでMeteor - 同じテンプレートヘルパーで別の関数を呼び出す方法

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

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ファイルに

。おかげで、

答えて

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

しかし、あなただけのヘルパーから機能を呼び出すためにしたいならば、あなたはこれがヘルパーブロックの外側で関数を定義する必要がありますどのようにそれを行うことができます。 JSファイルで

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

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

あなたは流星の機能をので、あなたの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

元のコードが失敗した理由何らかの理由で、私のHTMLファイルに

? Template.instance()。methodname()がうまくいかない理由 – droidbee

関連する問題