如何在模板中使用动态参数调用流星助手

时间:2015-09-13 22:29:30

标签: events meteor helpers

我有以下模板:

<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设置为模板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
     }

    });

谢谢你的帮助

1 个答案:

答案 0 :(得分: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应用程序中。

meteor add reactive-var