javascript - 我无法让 Handlebars 助手使用meteorjs 中的参数

标签 javascript meteor handlebars.js

我正在尝试在 MeteorJS 中使用带有参数的简单 Handlebars 助手。

让我给你举个例子:

{{#myList data className="myClassName"}}
  {{name}}
{{/myList}}

助手是这样声明的:

Handlebars.registerHelper('myList', function(context, options) {
  if (context && context.length){
    var className = options.hash.className || "", 
       ret = "<ul class='"+ className +"'>";

    for(var i=0, j=context.length; i<j; i++) {
      ret = ret + options.fn(context[i]);
    }

    return ret + "</ul>";
  }
});

但是属性“hash”始终是一个空数组。

如果我测试这样的版本:

{{#myList data "myClassName"}}
  {{name}}
{{/myList}}

回调方法永远不会收到第二个参数。

我是不是做错了什么?

最佳答案

来自meteor/handlebars :

In Meteor, block helpers do not take arbtirary positional and keyword arguments like non-block helpers. Instead, the arguments are treated together as a nested Handlebars helper invocation expression.

但是您应该能够传递关键字参数:

The exact rule is that a block helper is always invoked with either no arguments; one positional argument (and no keyword arguments); or only keyword arguments. The presence of any non-keyword argument, like foo in the previous example, causes all following positional and keyword arguments to be passed to foo (if it is a function, or else swallowed). Otherwise, if there are only keyword arguments, they are passed to the helper, so you can define a block helper that takes any number of arguments by giving them names: {{#helper x=1 y=2 z=3}}...{{/helper}}.

这应该有效:

{{#myList myData=data className="myClassName"}}
  {{name}}
{{/myList}}

关于javascript - 我无法让 Handlebars 助手使用meteorjs 中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17746786/

相关文章:

javascript - 如何使用 JS/jQuery 正确提交动态创建的表单?

javascript - mysql lite 数据库 Cordova

gulp - 模板已使用比当前运行时版本更早的Handlebars进行了预编译

javascript - Meteor {{#each}} – 每个项目的条件格式

javascript - HTML 到 JS 对象

javascript - 该程序无法显示jquery

javascript - 为什么这段 JavaScript 代码(作为 HTML 属性嵌入)不起作用?

ios - 升级到 El Capitán 后,Meteor 应用程序未在 iOS 模拟器中运行

javascript - meteor react 内容过滤

javascript - 事件代码在不同浏览器中的行为不同