简体   繁体   English

从Meteor模板向助手传递多个参数?

[英]Pass multiple arguments to helper from Meteor template?

this is my problem: I have an Array of Array: allAnswers and I want to display its rows in a table. 这是我的问题:我有一个数组数组:allAnswers,我想在表中显示其行。 The problem is that I want to pass a value while I'm calling it to set the right row in the right position of the table. 问题是我想在调用它以将正确的行设置在表的正确位置时传递一个值。 I've try this but it still not works. 我已经尝试过了,但是还是不行。

HTML file in template name= body 模板名称=正文中的HTML文件

<table>
    <tr>
       <th>Week/Questions</th>
       <th>Q1</th>
       <th>Q2</th>
       <th>Q3</th>
       <th>Q4</th>
     </tr>
     <tr>
       <td>Week</td>
       <td>{{allAnswers "0"}}</td>
       <td>{{allAnswers "1"}}</td>
       <td>{{allAnswers "2"}}</td>
       <td>{{allAnswers "3"}}</td>
    </tr>
</table>

JS File JS文件

Template.body.helpers({        
  allAnswers: function(N) {
      var i=N;
      for(i=0;i<10;i++){
            var allAnswers(i)=[];
            for(var j=0; j<4; j++){
                allAnswers(i)[j+1] = Quests.find({ userId: this._id, answer: { $exists: true }}).map((el) => el.answer[j]);
                alert(allAnswers(i)[j]);
                return allAnswer(i)[j];
            }
      }
  }
});

I'd recommend changing tack and using loop helpers in Blaze to build the table like so: 我建议更改方式并在Blaze中使用循环助手来构建表,如下所示:

<table>
    <tr>
       <th>Week/Questions</th>
       <th>Q1</th>
       <th>Q2</th>
       <th>Q3</th>
       <th>Q4</th>
     </tr>

     {{#each week in quests}}
     <tr>
       <td>Week</td>
       {{#each answer in week}}
         <td>{{answer}}</td>
       {{/each}}
    </tr>
    {{/each}}
</table>

JS File JS文件

Template.body.helpers({
  quests() {
    return Quests.find({
      userId: Meteor.user(),
      answer: { $exists: true }
    });
  }
});

The idea being that your helper returns the data cursor once, and then your template is responsible for structuring that data visually on the page. 这样的想法是,您的助手将一次返回数据游标,然后您的模板负责在页面上直观地构造数据。

The other approach would be to build your two dimensional array in Template.body.onCreated and then reference the data from there. 另一种方法是在Template.body.onCreated构建二维数组,然后从那里引用数据。 This would disable any reactivity and so the table won't update when the underlying data changes. 这将禁用任何反应性,因此当基础数据更改时,表将不会更新。


Important to note that helpers are run every time they are referenced in the template as well as every time a reactive data source they use changes (in this case, the Quests collection). 重要的是要注意,每次在模板中引用帮助程序时,以及每次他们使用反应性数据源使用更改时(在这种情况下,都是Quests集合),都将运行帮助程序。

Also a minor note that userId checks should be done in the data publication on the server side. 还有一点需要注意的是,应该在服务器端的数据发布中执行userId检查。 The Meteor tutorial has more information about that. 流星教程对此有更多信息。

I also strongly recommend this article if you are getting confused about data flow in Meteor: https://medium.com/meteor-js/data-flow-from-the-database-to-the-ui-three-layers-of-meteor-d5e208b466c3 如果您对Meteor中的数据流感到困惑,我也强烈推荐这篇文章: https : //medium.com/meteor-js/data-flow-from-the-database-to-the-ui-three-layers-of -流星-d5e208b466c3

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

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