问题描述
我想在事件处理程序中设置上下文数据,该事件处理程序将新上传的照片插入到图像FScollection中。 我要设置的是照片文件的新生成的ID。 我需要将此ID传递给子模板进行进一步处理。 以下是我正在使用的代码:
我应该如何定义以后要在事件处理程序中使用的数据?
images.js
Template.Profile.events({
'change #uploadBtn': function(event, template) {
var name = $("#uploadBtn").val();
$("#uploadFile").val(name);
FS.Utility.eachFile(event, function(file) {
Images.insert(file, function (err, fileObj) {
if (err){
// handle error
}
else {
//here I want to set fileObj._id as data for further usage.
}
});
});
}
});
Template.imageView.helpers({
images: function () {
return Images.findOne({_id: this.imageId});
}
});
Template.imageView.events({
'click #deleteBtn': function (event) {
this.remove();
}
});
模板文件
images.html
<template name="Profile">
<input id="uploadFile" placeholder="Choose File" disabled="disabled" style="cursor: auto; background-color: rgb(235, 235, 228); "/>
<div class="fileUpload btn btn-primary">
<span>Upload</span>
<input id="uploadBtn" type="file" name="…" class="upload">
</div>
{{> imageView}}
</template>
<template name="imageView">
<div class="imageView">
{{#if images}}
<div style="width: 120px;height: 120px;margin-bottom: 30px;">
<div style="float: middle;">
{{#with images}}
<button id="deleteBtn" type="button" class="btn btn-primary" >Delete</button>
{{/with}}
</div>
<a href="{{images.url}}" target="_blank" >
<img src="{{images.url}}" alt="" class="thumbnail" style="width: 96px; height: 96px;"/>
</a>
</div>
{{/if}}
</div>
</template>
1楼
您有两种选择。
- 在引用图像的对象中包含图像的_id
- 在图像本身中包含对象的_id作为parentId或类似名称
对于第一种情况,在Image.insert
的回调中,使用图像的_id,然后在父对象上执行.update()
(可能是用户吗?)以包括对图像的引用。
如果允许多个图像,则可能需要使用这些_id更新数组。
在第二种情况下,您可以使用所需的parentId从插入中更新刚插入到回调中的对象。
然后,您只需要在查询中包括有关从属图像的适当子句,由于具有反应性,它将自动更新。