如何评估空格键参数中的任意表达式?

时间:2015-02-13 18:21:56

标签: meteor spacebars

我喜欢说{{#if tiny || huge}}之类的内容,但看起来空格键只有一个真正的功能。什么是最好的解决方案?

我以为我会写另一个助手,但是如何从另一个助手的代码中调用一个助手呢? E.g。

Template.foo.helpers({ tiny: function() { return this < 10 },
                       huge: function() { return this > 1000 },
                       tinyOrHuge: function() { return tiny() || huge() } // bzzt!
})

我不想通过复制tinyOrHugetiny来重写huge。那不是很干。

是否有以编程方式访问辅助函数的名称?如果传入名称,helpers返回帮助程序会很好。例如。我可以致电Template.foo.helpers('tiny')()。但是我已经查看了这些Blaze.Template对象,并且没有看到在不访问私有变量的情况下获取函数的方法,例如Template.foo.__helpers.get('tiny')

1 个答案:

答案 0 :(得分:1)

这是一个在保持必要的上下文的同时重用辅助函数的示例:

var tiny = function() {
  return this < 10;
};

var huge = function() {
  return this > 1000;
};

Template.foo.helpers({
  tiny: tiny,

  huge: huge,

  tinyOrHuge: function() {
    return tiny.call(this) || huge.call(this);
  }
});