It’s probably a super simple javascript thing for most of you, but I cant seem to get it to work.
Situation
I have a form with four different file-input-fields for images:
- sideimage
- frontimage
- topimage
- bottomimage
My insert event for the sideimage looks like this:
Template.product_form_edit.events({
'change .fileinput--side': function(event, template) {
FS.Utility.eachFile(event, function(file) {
var tmpdoc = new FS.File(file);
tmpdoc.imageType = "sideimage";
Images.insert(tmpdoc, function (err) {});
});
Session.set('sideImageChangeRequest', null);
}
});
This works like expected.
Problem
Now if I want to create the events for the other images I would copy and paste this whole code-block and just change the “sideimage” and “sideImageChangeRequest”. So basically I repeat most of the code four times.
Example:
My code for the frontimage would look like this:
Template.product_form_edit.events({
'change .fileinput--front': function(event, template) {
FS.Utility.eachFile(event, function(file) {
var tmpdoc = new FS.File(file);
tmpdoc.imageType = "frontimage";
Images.insert(tmpdoc, function (err) {});
});
Session.set('frontImageChangeRequest', null);
}
});
So lots of repeating myself here.
How can I make this more efficient?
Thanks for any push in the right direction.