我可以使用全局帮助器将数据传递给onRendered吗?

时间:2021-09-26 16:52:42

I'm trying to set a variable in the onRendered callback equal to the value from the active global helper. My global helpers are defined as follows:

我正在尝试在onRendered回调中设置一个等于活动全局帮助器中的值的变量。我的全球助手定义如下:

client/lib/helpers.js

//all possible calculations//
getResults = function(valuationId,targetId){
    var valuation = Valuations.findOne({_id: valuationId});
    var targetTicker = Companies.findOne({_id:targetId}).ticker;
    var targetData = CompaniesData.findOne({ticker: targetTicker});
    return {
        peFy1: targetData.epsFy1 * valuation.priceEarningsFy1,
        peFy2: targetData.epsFy2 * valuation.priceEarningsFy2
        //more//
    }
};

//choose one value from above, based on several other variables//
Template.registerHelper('active',function(){
    var valuationId = this._id;
    var targetId = this.targetId;
    var valuationPeriod = this.valuationPeriod;
        switch (valuationPeriod) {
            case "FY1"
                return getResults(valuationId, targetId).peFy1;
                break;
            case "FY2":
                return getResults(valuationId, targetId).peFy2;
                break;
        //more cases//

I can get and use a single value directly from getResults with the last line below.

我可以直接从getResults获取并使用单个值,最后一行。

Template.ValuationBase.onRendered (function () {
    var targetId = Template.parentData(0).targetId;
    var valuationId = Template.parentData(0)._id;
    var valuationActive = getResults(valuationId,targetId).peFy1;

But I can't figure out how to get the value resulting from logic in the active helper. I thought it would be var valuationActive = active(); but that returns not defined. I also saw this answer but it seems to be creating the function in the template JS, rather than referring to a helper.

但我无法弄清楚如何从活动帮助器中获取逻辑产生的值。我以为它会是var valuationActive = active();但返回没有定义。我也看到了这个答案,但似乎是在模板JS中创建函数,而不是引用帮助器。

I'm sure this is a basic mistake in how I am calling the function. Thank you.

我确定这是我调用函数的一个基本错误。谢谢。

1 个解决方案

#1


0  

Use Blaze._globalHelpers.active() to refer to your global helper active from javascript anywhere on the client.

使用Blaze._globalHelpers.active()在客户端的任何位置引用从javascript激活的全局帮助程序。

I would add (again) that relying on this to pass the data context for your global helper is risky. You're assuming that context will always be set correctly no matter what. You can defend yourself from this assumption with:

我会再次添加(依赖于此)来传递全局帮助程序的数据上下文是有风险的。您假设无论如何都始终正确设置上下文。您可以通过以下方式保护自己免受此假设:

Template.registerHelper('active',function(id){
  var valuationId = id || this._id; // use the arg if provided, otherwise rely on 'this'
  ...
});

#1


0  

Use Blaze._globalHelpers.active() to refer to your global helper active from javascript anywhere on the client.

使用Blaze._globalHelpers.active()在客户端的任何位置引用从javascript激活的全局帮助程序。

I would add (again) that relying on this to pass the data context for your global helper is risky. You're assuming that context will always be set correctly no matter what. You can defend yourself from this assumption with:

我会再次添加(依赖于此)来传递全局帮助程序的数据上下文是有风险的。您假设无论如何都始终正确设置上下文。您可以通过以下方式保护自己免受此假设:

Template.registerHelper('active',function(id){
  var valuationId = id || this._id; // use the arg if provided, otherwise rely on 'this'
  ...
});