我循环查看信用卡交易清单,需要致电信用卡处理机以获取当前状态。我试图使用一个调用方法的帮助器,但它不会在模板上呈现。如果我登录到控制台,我可以看到结果正确返回。
<tbody>
{{#each donations}}
<tr>
<td>{{ transactionId }} {{ status transactionId }}</td>
<td>{{ donorName donor }}</td>
<td>{{ donorEmail donor }}</td>
<td>{{ formatMoney amount }}</td>
<td>{{ createdAt }}</td>
</tr>
{{/each}}
</tbody>
拉取数据:
donations: function() {
return Donations.find({
type: 'Credit Card',
createdAt: { $gte: new Date('Jan 1, 2015'), $lt: new Date('Jun 30, 2015') }
});
},
status
帮助者:
status: function(transactionId) {
var transaction = Meteor.call('getTransactionInfo', transactionId, function(error, result) {
console.log(result);
return result.status;
});
}
方法:
getTransactionInfo: function(transactionId) {
var transaction = Meteor.wrapAsync(gateway.transaction.find, gateway.transaction);
var response = transaction(transactionId);
return response;
},
我发现有几个问题涉及putting the data in a ReactiveVar
或Session
变量,但我不知道如何使用它,因为我正在为循环的每个实例返回信息
答案 0 :(得分:2)
您可以创建新模板donation
。表中的每一行都是它自己的donation
实例,如下所示:
<tbody>
{{#each donations}}
{{> donation }}
{{/each}}
</tbody>
现在在onCreated
模板的donation
内,创建一个ReactiveVar
。然后立即调用该方法,并在方法的回调中设置ReactiveVar
。由于每一行都是自己的模板实例,因此每一行最终都会有自己的ReactiveVar
。
Template.donation.onCreated(function () {
var status = new ReactiveVar(null);
this.status = status;
Meteor.call('getTransactionInfo', this.data.transactionId, function (err, result) {
if (err) { /* handle it */ }
status.set(result);
});
});
Template.donation.helpers({
status: function () { return Template.instance().status.get(); },
/* rest of the helpers */
});
现在,首次创建模板时,方法调用仍在进行中,因此status.get()
会返回null
。我们必须对此进行测试并显示加载微调器。方法调用完成后,status.get()
将被设置为结果,因此微调器将替换为值。
<tr>
<td>
{{ transactionId }}
{{#if status}}
{{ status }}
{{else}}
loading... <!-- or display a spinner -->
{{/if}}
</td>
<td>{{ donorName donor }}</td>
<td>{{ donorEmail donor }}</td>
<td>{{ formatMoney amount }}</td>
<td>{{ createdAt }}</td>
</tr>
答案 1 :(得分:0)
From a security pov you've just exposed a lookup to your bank's systems from your (untrusted) clients. Anyone can run Meteor.call('getTransactionInfo', this.data.transactionId...)
from their browser and that will end up hitting your bank. Great DDOS vector. Even absent malicious users these computations will end up running repeatedly for every transaction.
Better approach: run a cron job on your server that periodically checks the status of transactions (until they have cleared and no longer need checking) and put those stati into your donation collection.