【问题标题】:How to call meteor helpers with dynamic params within a template如何在模板中使用动态参数调用流星助手
【发布时间】:2015-12-09 21:16:05
【问题描述】:

我有以下模板:

<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); 
     }

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

    });

谢谢你的帮助

【问题讨论】:

  • 嘿@bouraoui {{#each}} 目前只接受数组、游标或虚假值。您不能将参数传递给 {{#each}} 块。

标签: events meteor helpers


【解决方案1】:

你可以像这样使用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

【讨论】:

  • @benstr 我试了一下,代码运行良好,我解决了问题:)
猜你喜欢
  • 1970-01-01
  • 2018-05-19
  • 1970-01-01
  • 2015-11-28
  • 2018-03-03
  • 2015-04-15
  • 1970-01-01
  • 1970-01-01
  • 2017-01-10
相关资源
最近更新 更多