Meteor:来自另一个帮助者的访问模板助手(或变量)

Hab*_*bib 22 meteor spacebars meteor-blaze meteor-helper

如何从另一个模板助手中引用模板助手?例如...

Template.XXX.helpers({
    reusableHelper: function() {
        return this.field1 * 25 / 100; //or some other result
    },
    anotherHelper: function() {
        if (this.reusableHelper() > 300) //this does not work
            return this.reusableHelper() + ' is greater than 300'; 
        else
            return this.reusableHelper() + ' is smaller than 300';
    }
});
Run Code Online (Sandbox Code Playgroud)

我也尝试过Template.instance().__ helpers.reusableHelper - 一切都没有运气.

或者有没有办法定义反应模板实例变量?

XXX是一个在同一页面上呈现多次的子模板.

ben*_*str 13

您只能使用全局模板帮助程序.

Blaze._globalHelpers.nameOfHelper()

这是一个调用Iron的例子:路由器的路径全局帮助器.

Template.ionItem.helpers({
  url: function () {
    var hash = {};
    hash.route = path;
    hash.query = this.query;
    hash.hash = this.hash;
    hash.data = this.data;
    var options = new Spacebars.kw(hash);

    if (this.url){
      return Blaze._globalHelpers.urlFor(options)
    } else if( this.path || this.route ) {
      return Blaze._globalHelpers.pathFor(options)
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

编辑:你的第二个问题.您可以在页面上多次调用相同的模板,并将不同的数据属性直接传递给它,和/或使用#each块模板包装器来迭代数据.#each将多次调用模板,每次都给它一个不同的数据上下文.

#each示例

<template name="listOfPosts">
  <ul>
    {{#each posts}}
      {{>postListItem}} <!--this template will get a different data context each time-->
    {{/each}}
  </ul>
</template>
Run Code Online (Sandbox Code Playgroud)

属性示例

<template name="postDetails">
  {{>postHeader title="Hello World" headerType="main" data=someHelper}}
  {{>postHeader title="I am a sub" headerType="sub" data=newHelper}}
  {{>postBody doc=bodyHelper}}
</template>
Run Code Online (Sandbox Code Playgroud)


ajd*_*uke 12

这就像使用通用代码一样,您可以创建另一个包含可重用代码的javascript函数,并从您需要的任何地方调用它.

就像你的代码一样 -

function calcField(field){
   return field * 25 / 100
}
Run Code Online (Sandbox Code Playgroud)

并在你的模板助手 -

Template.XXX.helpers({
    reusableHelper: function() {
        return calcField(this.field1); 
    },
    anotherHelper: function() {
        if (calcField(this.field1) > 300) 
            return calcField(this.field1) + ' is greater than 300'; 
        else
            return calcField(this.field1) + ' is smaller than 300';
    }
});
Run Code Online (Sandbox Code Playgroud)

或者有没有办法定义反应模板实例变量?

您可以使用Session变量Reactive变量


Lee*_*son 5

免责声明:这可能不会直接回答您的问题,但对于遇到类似用例的人可能会有所帮助:

有时很容易被锁定在"Meteor方式"中,标准的Javascript规则被遗忘了.

两个用例听起来与您尝试的类似:

1.对于可以在客户端的任何位置访问的帮助程序/事件,只需设置全局帮助程序即可.

把它放进去,比方说client/helpers.js:

Helpers = {
    someFunction: function(params) {
        /* Do something here */
    }
}
Run Code Online (Sandbox Code Playgroud)

现在Helpers.someFunction()可用于所有模板.

如果由于某种原因想要将本地模板实例绑定到它,那么它是标准的JS:

var boundFunction = Helpers.someFunction.bind(this);
Run Code Online (Sandbox Code Playgroud)

2.要模板创建可重用的Blaze助手,请使用Template.registerHelper

例如,此函数使用"数字"库来格式化数字:

Template.registerHelper('numeral', function(context, opt) {
    var format = (opt.hash && opt.hash.format) || '0,0.00';
    return numeral(context || 0).format(format);
});
Run Code Online (Sandbox Code Playgroud)

你可以在任何模板中使用它,如下所示:

{{numeral someNumberVariable format='0,0'}}
Run Code Online (Sandbox Code Playgroud)