我正试图在我的一个表单中实现JQuery滑块,并使用ReactiveVar来跟踪它的值
代码:
Template.requestPaymentForm.created = function() {
this.requestAmountValue = new ReactiveVar(0);
};
Template.requestPaymentForm.helpers({
selectedRequestAmount: function() {
var value = Template.instance().requestAmountValue.get() / 100;
return value;
},
...
Template.requestPaymentForm.events({
'slide #requestAmountSlider': function(event, template) {
var sliderValue = template.$("#requestAmountSlider").slider("option","value");
template.requestAmountValue.set(sliderValue);
},
});
HTML
<template name='requestPaymentForm'>
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Request Payment</h4>
</div>
{{#autoForm collection="Payments" id="insertPaymentForm" type="method" meteormethod="createPayment"}}
<div class="modal-body">
<div class="amounts">
<span class="selected amount">Amount: {{formatMoney selectedRequestAmount}}</span>
<span class="available amount">Available: {{formatMoney availableForRequest}}</span>
</div>
<br>
<div id="requestAmountSlider"></div><br>
{{> afFieldInput name='amount'}}
</div>
<div class="modal-footer">
<button id="submitRequestPaymentForm" type="submit" class="btn btn-default btn-primary">Submit</button>
</div>
{{/autoForm}}
</div>
</template>
但是,当我渲染此模板时,我收到错误
TypeError: Cannot read property 'get' of undefined
at Object.Template.requestPaymentForm.helpers.selectedRequestAmount
知道为什么会这样吗?
非常感谢
答案 0 :(得分:0)
这是Autoform插件的错误。来自 Template.requestPaymentForm.created 的所有初始化都将被取消,并被Autoform .created覆盖。因此,它表示变量在helper中是未定义的。
我遇到了同样的问题,我正在等待autoform团队解决这个问题。我的修复是添加一个检查帮助器,如果它是null,则赋值。希望这有帮助。