javascript - Can I use a global helper to pass data into onRendered? -


i'm trying set variable in onrendered callback equal value active global helper. global helpers defined follows:

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 1 value 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 , use single value directly getresults last line below.

template.valuationbase.onrendered (function () {     var targetid = template.parentdata(0).targetid;     var valuationid = template.parentdata(0)._id;     var valuationactive = getresults(valuationid,targetid).pefy1; 

but can't figure out how value resulting logic in active helper. thought var valuationactive = active(); returns not defined. saw this answer seems creating function in template js, rather referring helper.

i'm sure basic mistake in how calling function. thank you.

use blaze._globalhelpers.active() refer global helper active javascript anywhere on client.

i add (again) relying on this pass data context global helper risky. you're assuming context set correctly no matter what. can defend assumption with:

template.registerhelper('active',function(id){   var valuationid = id || this._id; // use arg if provided, otherwise rely on 'this'   ... }); 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

android - Go back to previous fragment -