简体   繁体   中英

Passing variables into Meteor helpers

I am struggling with what I know is a very basic question related to variable declaration. I've read everything I can find on variables but I don't know if my problem is related to 1) how I am declaring variables or 2) how I am setting the scope of the variables.

To start, my understanding of variables in Meteor is that if I use var , then I am setting file-scope, which would make that variable available to every helper for that particular template. If I do not use var , it will be global and therefore available to the helpers in every template. Is that correct?

The following block of code works fine, returning the correct value in the client:

Template.CompanyFinancials.helpers({
    priceEarningsFy1: function () {
        var compTicker = this.ticker
        var price = Companies.findOne({ticker: compTicker}).capTable.lastClose;
        var epsFy1 = Companies.findOne({ticker: compTicker}).fy1.eps;
        return (price / epsFy1).toFixed(1)
});

I have dozens of similar calculations throughout this app and many which rely on more variables than this example, so I have been trying to factor out the variables and reuse them in the template, like so:

var compTicker = function() {
    return this.ticker;
};
console.log(compTicker);
var price = function(compTicker) {
    Companies.findOne({ticker: compTicker}).capTable.lastClose;
};
console.log(price);
var epsFy1 = function(compTicker) {
    Companies.findOne({ticker: compTicker}).fy1.eps;
};
console.log(epsFy1);

Template.CompanyFinancials.helpers({
    priceEarningsFy1: function (price, epsFy1) {
         return (price / epsFy1).toFixed(1)
    }
});

With this code, console.log() actually returns the text within each function (eg, return this.ticker ) for each variable, not the value. If I declare the variables without functions, like I've done within the helper, it returns undefined for compTicker .

I tried to follow this answer which explains reusable code, but not clear if same use case applies. My variables point to specific fields in the database, not necessarily calculations.

Can anyone help me repair my syntax? I'm writing multiples more code than I need to with my current understanding. Thank you.

EDIT I also tried declaring the variables the same way they are declared in the helper, but these return undefined .

var compTicker = this.ticker;
console.log(compTicker);
var price = CompaniesFeed.findOne({ticker: this.ticker}).capTable.lastClose;
console.log(price);
var epsFy1 = CompaniesFeed.findOne({ticker: this.ticker}).fy1.eps;
console.log(epsFy1);

RESOLUTION: Using global helpers and returning multiple values, then using dot notation to access in the template HTML:

Template.registerHelper('priceEarnings',function(){
    var ticker = this.ticker;
    var company = CompaniesFeed.findOne({ticker: ticker});
    return {
        peFy1: (company.capTable.lastClose / company.financial.fy1.eps).toFixed(1),
        peFy2: (company.capTable.lastClose / company.financial.fy2.eps).toFixed(1)
    };
});

<td>{{priceEarnings.peFy1}}x</td>

You might be looking for global helpers . These are helpers which can be reused across all templates.

For your priceEarningsFy1 function for example:

Template.registerHelper('priceEarningsFy1',ticker => {
  const company = Companies.findOne({ticker: ticker});
  return ( company.capTable.lastClose / company.fy1.eps ).toFixed(1);
});

In this case I've specified that ticker is to be provided as an argument. From a blaze template you would use {{priceEarningsFy1 this.ticker}} for example. To refer to this function from js code use UI._globalHelpers.priceEarningsFy1(ticker)

Note that any local functions you define inside a given file are available to any other functions inside the same file. My pattern is to put all my global helpers in one file sorted by name and then at the bottom add various utility functions for use by the global helpers. This keeps things relatively dehydrated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM