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

How to call meteor helpers with dynamic params within a template

我有以下模板:

<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 以动态设置参数到在模板 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
     }

    });

感谢您的帮助

您可以像这样使用 ReactiveVars :

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