【发布时间】:2017-09-23 15:42:39
【问题描述】:
我想包含一个带有参数的 Blaze 模板,然后在事件中使用参数值。问题是,当我第二次使用不同的参数包含模板时,我会从事件中模板的第一个实例获取参数值。
模板:
<template name="UploadFormLayoutImage">
<form class="uploadPanel">
<input type="file" name="fileupload" id="input-field">
<label for="input-field">Upload file</label>
</form>
</template>
包括:
{> UploadFormLayoutImage layoutArea="area1"}}
{> UploadFormLayoutImage layoutArea="area2"}}
js:
Template.UploadFormLayoutImage.onCreated(function(){
this.currentArea = new ReactiveVar;
this.currentArea.set(this.data.layoutArea);
});
Template.UploadFormLayoutImage.helpers({
layoutArea: function() {
return Template.instance().currentArea.get(); //Returns the correct argument value for each instance of the template.
}
});
Template.UploadFormLayoutImage.events({
'change input[type="file"]': function(e, instance) {
e.preventDefault();
console.log(instance.data.layoutArea); //Allways returns 'area1'
}
});
我在这里缺少什么? (这是我的第一个 Stackoverflow 问题。请温柔:))
【问题讨论】:
-
请分享 UploadFormLayoutImage 模板。您是否使用此模板中的参数调用助手?
-
@Ankit 我已经添加了模板。我实际上不需要模板中的助手。我只需要在事件中获得与助手中相同的值。我将在创建 Mongo 文档时使用该值。感谢您的评论。
标签: meteor meteor-blaze