gpt4 book ai didi

events - 如何在模板中使用动态参数调用 meteor 助手

转载 作者:行者123 更新时间:2023-12-01 14:02:49 26 4
gpt4 key购买 nike

我有以下模板:

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

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

helper :

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

});

我想定义一个 DOM 上的点击,以动态地将参数设置为在模板 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
}

});

谢谢你的帮助

最佳答案

您可以像这样使用 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 应用中。

meteor add reactive-var

关于events - 如何在模板中使用动态参数调用 meteor 助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32555168/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com