流星:从另一个帮助器访问模板帮助器(或变量) [英] Meteor: Access Template Helper (or variable) from another helper

查看:60
本文介绍了流星:从另一个帮助器访问模板帮助器(或变量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从另一个引用模板帮助器?例如...

How can I reference a template helper from another one? For example...

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

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

I have also tried Template.instance().__helpers.reusableHelper - all with no luck.

或者有没有一种方法可以定义反应式模板实例变量?

Alternatively is there a way to define reactive Template instance variables?

XXX是一个子模板,可在同一页面上多次渲染.

XXX is a sub-template that renders multiple times on the same page.

推荐答案

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

This like using of common code, you can make another javascript function which contains the your reusable code and call it from wherever you required.

就像输入您的代码一样

Like in your code-

function calcField(field){
   return field * 25 / 100
}

以及您的模板助手中-

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

或者有一种方法可以定义反应式Template实例 变量?

Alternatively is there a way to define reactive Template instance variables?

您可以使用会话变量 查看全文

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