简体   繁体   English

将变量传递给流星助手

[英]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. 我已经阅读了所有可以在变量上找到的内容,但是我不知道我的问题是否与1)我如何声明变量或2)如何设置变量的范围有关。

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. 首先,我对Meteor中的变量的理解是,如果我使用var ,那么我将设置文件作用域,这将使该变量可用于该特定模板的每个助手。 If I do not use var , it will be global and therefore available to the helpers in every template. 如果我不使用var ,它将是全局的,因此每个模板中的助手都可以使用。 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. 使用此代码,console.log()实际上为每个变量而不是值return this.ticker每个函数内的文本(例如, return this.ticker )。 If I declare the variables without functions, like I've done within the helper, it returns undefined for compTicker . 如果我声明的变量没有函数,就像我在助手中所做的那样,它将为compTicker返回undefined。

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 . 编辑我也尝试过用在帮助器中声明变量的方式声明变量,但是这些变量返回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: 解决方案:使用全局帮助器并返回多个值,然后使用点表示法访问模板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: 以您的priceEarningsFy1函数为例:

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. 在这种情况下,我已指定将ticker作为参数提供。 From a blaze template you would use {{priceEarningsFy1 this.ticker}} for example. 例如,在{{priceEarningsFy1 this.ticker}}模板中,您可以使用{{priceEarningsFy1 this.ticker}} To refer to this function from js code use UI._globalHelpers.priceEarningsFy1(ticker) 要从js代码引用此函数,请使用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. 这使东西相对脱水。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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