2015-09-13 248 views
3

我有以下模板:如何調用模板內的動態PARAMS流星助手

<template name="tempName" > 
    {{#each helperOne "1" "2" }} 

    <p>Lorem upsom</p> 
    {{#each}} 
    <a id="next"></a> 
</template> 

助手:

template.tempName.helpers({ 
    "helperOne":function(a,b) 
    { 
     console.log("a = "+a+" b ="+b); 
    } 

}); 

我要定義一個DOM點擊動態的設置params to helpers在模板tempName上定義值爲「1」和「2」

template.tempName.events({ 
    "click .next":function(e,tmp) 
    { 
     //how change dynamically a and b of the helperOne helper into the 
     // template defintion of tempName 
    } 

    }); 

感謝您的幫助

+0

hey @bouraoui {{#each}}目前只接受數組,遊標或falsey值。你不能將params傳遞給{{#each}}塊。 – benstr

回答

2

你可以使用ReactiveVar就像這樣:

JS

Template.tempName.onCreated(function(){ 
    this.arg1 = new ReactiveVar("1"); 
    this.arg2 = new ReactiveVar("2"); 
}); 

Template.tempName.helpers({ 
    helperOne: function(arg1, arg2){ 
    console.log("helperOne called with", arg1, arg2); 
    }, 
    arg1: function(){ 
    return Template.instance().arg1.get(); 
    }, 
    arg2: function(){ 
    return Template.instance().arg2.get(); 
    } 
}); 

Template.tempName.events({ 
    "click .next": function(event, template){ 
    template.arg1.set("whatever"); 
    template.arg2.set("you want"); 
    } 
}); 

HTML

<template name="tempName" > 
    {{#each helperOne arg1 arg2}} 
    <p>Lorem ipsum</p> 
    {{#each}} 
    <a class="next"></a> 
</template> 

不要忘了包添加到您的流星應用。

meteor add reactive-var 
+0

#each不接受params – benstr

+0

@benstr我試試看,代碼運行良好,我解決了問題:) –

+0

@saimeunt感謝您的解決方案 –