2015-09-13 114 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感谢您的解决方案 –