简体   繁体   English

切换到Meteor模板助手

[英]Switch in Meteor template helper

I have a template with a table created by iterating over an array of user selected companies, which are stored, by ticker symbol, in a document attribute called selections . 我有一个带有表格的模板,该表格是通过遍历一系列用户选择的公司而创建的,这些公司通过股票代码存储在称为selections的文档属性中。 I show different values associated with each company, depending on a different user selection, called metric . 我会根据不同的用户选择(称为metric显示与每个公司相关的不同值。

I am having trouble writing a helper with the if/else statement required to change the value depending on the user selection. 我在编写需要根据用户选择更改值的if / else语句时遇到麻烦。 With the code below, headingNum and headingDen are showing properly. 使用下面的代码, headingNumheadingDen正确显示。 So is companyName which is associated with an individual selection. 与单个选择相关联的companyName也是如此。 If I replace valuationNum with this.reported.capTable.enterpriseValue , the correct value appears. 如果我用this.reported.capTable.enterpriseValue替换valuationNumthis.reported.capTable.enterpriseValue显示正确的值。 But I cannot get it to appear when using the helper. 但是我无法在使用帮助程序时显示它。

<template name="Table">
    <div>
        <table>
            <thead>
                <tr>
                    <th>Company</th>
                    <th>{{headingNum}}</th>
                    <th>{{headingDen}}</th>
                </tr>
            </thead>
            <tbody>
                {{#each selections}}
                    <tr>
                        <td>{{companyName}}</td>
                        <td>${{valuationNum}}</td>
                        <td>${{valuationDen}}</td>
                    </tr>
                {{/each}}
            </tbody>
        </table>
    </div>
</template>

JS file JS文件

var metric = this.metric;
var period = this.period;
Template. Table.helpers({
    selections: function () {
        var selected = this.selections;
        return Companies.find({ticker: {$in: selected}})
    },
    headingNum: function () {
        switch (metric) {
            case "A":
                return "EV";
                break;
            case "B":
                return "Price";
                break;
            default:
                return "EV"
        }
    },
    valuationNum: function() {
        switch (metric) {
            case "A":
                return this.reported.capTable.enterpriseValue;
                break;
            case "B":
                return this.reported.capTable.lastClose;
                break;
            default:
                return ""
        }
    }
});

I tried breaking out the {{#each}}{{each}} block into a new template to see if it would help with the data context but no luck (and it messes up the table). 我尝试将{{#each}} {{each}}块分解为一个新模板,以查看它是否对数据上下文有帮助,但没有运气(它弄乱了表)。

Am I writing these helpers correctly? 我写的这些助手正确吗? I also receive an error message in the JS file saying reported is an unresolved variable even though that is the correct path. 我还在JS文件中收到一条错误消息,说reported是一个未解决的变量,即使那是正确的路径。

Thank you. 谢谢。

EDIT: 编辑:

This helper works, not sure why the other doesn't: 此帮助程序有效,不确定另一个为什么不这样做:

headingNum: function () {
    var metric = this.metric;
    switch (metric) {
        case "EV/EBITDA":
            return "EV";
            break;
        case "Price/Earnings":
            return "Price";
            break;
        default:
            return ""
    }
}

Meteor's template is built on blaze , which is reactive by design. 流星的模板建立在blaze之上,后者根据设计具有反应性。 This means that the template will only re-render when the data dependency values change. 这意味着仅当数据依赖项值更改时,模板才会重新呈现。 In this example, only when you modify Companies . 在此示例中,仅当您修改Companies However, when you modify the value of metric , the template will not re-render, which is why you are not seeing the change you expect. 但是,当您修改metric的值时,模板将不会重新呈现,这就是为什么您看不到预期的更改的原因。

The reactive way of achieving your goal is actually putting in the metric switch logic right inside the template: 实现目标的反应方式实际上是在模板内部放置度量标准切换逻辑:

{{#each selections}}
    <tr>
        <td>{{companyName}}</td>
        {{#if metricA}}
        <td>${{reported.capTable.enterpriseValue}}</td>
        {{else}}
        <td>${{reported.capTable.lastClose}}</td>
        {{/if}}
        <td>${{valuationDen}}</td>
    </tr>
{{/each}}

Inside your JS: 在您的JS中:

Template. Table.helpers({
    metricA: function() {
        return this.metric == 'A';
    }
})

Just having a quick browse over your code, just a few things. 只需快速浏览一下代码,就可以了解几件事。

You have set the metric and period variables at the window scope - not the template scope. 您已在窗口范围而非模板范围设置了度量和期间变量。 This is why your unable to access the metric variable within your helper. 这就是为什么您无法访问助手中的指标变量的原因。 To fix, just remove the var from where you initially create the metric variable. 要解决此问题,只需从最初创建metric变量的位置删除var。 This however, is not a good way to manage the scoping and variable management. 但是,这不是管理范围和变量管理的好方法。

I would say to make this really effective, you want to have a helper and event handler to track the getting and setting of the metric reactively (reactive-var package helps for this) 我想说,要使其真正有效,您需要一个帮助器和事件处理程序来以反应方式跟踪度量的获取和设置(reactive-var软件包对此有所帮助)

Setup the metric reactive variable 设置指标反应变量

Template.Table.created = function() {
  this.metric = new ReactiveVar();                                      
}

Get the Metric 获取指标

Template.Table.helpers({
  getMetric: function() {
    return this.metric.get();
  }
})

Set the Metric 设置指标

Template.Table.events({
  'click .setMetric': function(event, template) {
    event.preventDefault();
    var metric = $(event.target).data('metric');
    if(metric) template.data.metric.set(metric);
  }
});

Then using these, as a strategy you could simply have a helper for your headings and values - determining the metric on demand and restricting it. 然后使用这些作为策略,您可以简单地为标题和值提供帮助-确定按需度量并加以限制。

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

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