[英]Pass multiple arguments to helper from Meteor template?
这是我的问题:我有一个数组数组:allAnswers,我想在表中显示其行。 问题是我想在调用它以将正确的行设置在表的正确位置时传递一个值。 我已经尝试过了,但是还是不行。
模板名称=正文中的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文件
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];
}
}
}
});
我建议更改方式并在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文件
Template.body.helpers({
quests() {
return Quests.find({
userId: Meteor.user(),
answer: { $exists: true }
});
}
});
这样的想法是,您的助手将一次返回数据游标,然后您的模板负责在页面上直观地构造数据。
另一种方法是在Template.body.onCreated
构建二维数组,然后从那里引用数据。 这将禁用任何反应性,因此当基础数据更改时,表将不会更新。
重要的是要注意,每次在模板中引用帮助程序时,以及每次他们使用反应性数据源使用更改时(在这种情况下,都是Quests集合),都将运行帮助程序。
还有一点需要注意的是,应该在服务器端的数据发布中执行userId检查。 流星教程对此有更多信息。
如果您对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.