如何在流星的助手中使用多个参数? [英] How to use multiple parameters in a handlebar helper with meteor?

查看:90
本文介绍了如何在流星的助手中使用多个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Meteor创建一个自定义助手。继此文档之后:
https://github.com/meteor/meteor/wiki / Handlebars



我试图按以下方式定义我的帮手:

  Template.myTemplate.testHelper = function(foo,bar,options){
console.log(foo);
console.log(bar);
}

我的模板如下所示:

 < template name =myTemplate> 
{{#testHelpervalue1value2}}
{{/ testHelper}}
< / template>

查看我的控制台输出,我期望看到两行输出:

  value1 
value2

然而,我的控制台看起来像:

  value1 
函数(数据){
// don当数据相同时,不会创建虚假注释
//像以前一样(或者在`window`和
//`undefined`之间转换时)
if((data || Handlebars._defaultThis )===
(old_data || Handlebars._defaultThis))
return fn(data);
else
返回Spark.setDataContext(data,fn(data));
}

请注意,我对流星和句柄都是全新的。我认为使用下划线会更快乐,但流星浏览的文档几乎全部强调了下划线。我做错了定义我的帮助函数吗?它似乎没有看到第二个参数bar,而是将其解释为选项。 (注:如果我console.log(选项)它返回'未定义')。

流星版本0.4.0(8f4045c1b9)

解决方案

您的逻辑很好,只需对模板进行一些更改

 < template name =myTemplate> 
{{testHelpervalue1value2}}
< / template>请注意,testHelper函数仅在myTemplate模板中定义。


$ / code>


$ b

如果你想在全球范围注册testHelper,你需要做这样的事情。

 把手.registerHelper('testHelper',function(foo,bar){
console.log(foo);
console.log(bar);
});

玩得开心


I am trying to create a custom helper using Meteor. Following to the doc here: https://github.com/meteor/meteor/wiki/Handlebars

I have tried to define my helper as follows:

Template.myTemplate.testHelper = function(foo, bar, options) {
    console.log(foo);
    console.log(bar);
}

My template looks like:

<template name="myTemplate">
    {{#testHelper "value1" "value2"}}
    {{/testHelper}}
</template>

Looking at my console output, I expected to see 2 lines of output:

value1
value2

However my console looks like:

value1
function (data) {
    // don't create spurious annotations when data is same
    // as before (or when transitioning between e.g. `window` and
    // `undefined`)
    if ((data || Handlebars._defaultThis) ===
        (old_data || Handlebars._defaultThis))
      return fn(data);
    else
      return Spark.setDataContext(data, fn(data));
  } 

Note, I am completely new to meteor, and to handlebars. I think I would be much happier using underscore, but the documentation for meteor glances over underscore almost entirely. Am I doing something wrong defining my helper function? It seems that it is not seeing the second parameter "bar", and instead interpreting that as the options. (Note: if I console.log(options) it returns 'undefined').

Meteor version 0.4.0 (8f4045c1b9)

解决方案

Your logic is good, just make some changes to the template

<template name="myTemplate">
  {{testHelper "value1" "value2"}}
</template>

Bare in mind that the testHelper function is only defined in the myTemplate template.

If you want to register testHelper globally you'll need to do something like this

Handlebars.registerHelper('testHelper', function(foo, bar){
  console.log(foo);
  console.log(bar);
});

Have fun

这篇关于如何在流星的助手中使用多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆