I have a javascript function that generates an email.
If there are attachments to the email, I'm loopinp through the attachments and I upload each time one attchments to Dropbox and instead of actually attaching the attachment I append to the body of the email a Dropbox link to the file.
(The relevant code is wrapped with backslashes)
self.email = function (action, contact) {
var emailTo = contact.email();
var attachment = "";
var lineBreak = "%0D%0A";
var signature = uvm.user().fullName()
var body;
var dropboxLink = "";
switch (action) {
case "a":
body = "aaaa";
break;
case "b": case "c":
body "bbbccc";
break;
//////////The piece of code that I'm working on//////////
default:
$.each(self.checkedDocs(), function (key, doc) {
self.service.getDropboxLink(doc, function (result) {
dropboxLink = result;
attachment += lineBreak + doc.documentDescription() + ": " + dropboxLink;
});
});
body = "Please click on the link(s) below to view your document(s): "
//////////End//////////
}
attachment = attachment ? attachment + lineBreak + lineBreak : lineBreak;
body += lineBreak + attachment + signature;
window.location.href = "mailto:" + emailTo + "?subject=" + self.subject() + "&body=" + body;
}
And this is the getDropboxLink
function:
self.getDropboxLink = function (doc, callback) {
$.ajax({
url: "/API/dropbox/DropboxUpload",
type: "POST",
data: ko.toJSON(doc),
contentType: "application/json",
success: function (data) {
callback(data);
}
});
}
This code is not working properly, if the case is default
and there are emails attachments, the email is generaed before I get the Dropbox link from the API.
I can add async=false
to the POST request and that will solve the issue, but async=false
is deprecated.
I think I should do it with a callback or a promise but it's complicated becasue the part of the code that generates the email belongs to all the cases
of the switch
and the call to the API is only on the default
part.
If action
is default
I need:
attachment = attachment ? attachment + lineBreak + lineBreak : lineBreak;
body += lineBreak + attachment + signature;
window.location.href = "mailto:" + emailTo + "?subject=" + self.subject() + "&body=" + body;
to occur after this is done:
$.each(self.checkedDocs(), function (key, doc) {
self.service.getDropboxLink(doc, function (result) {
dropboxLink = result;
attachment += lineBreak + doc.documentDescription() + ": " + dropboxLink;
});
});
if the action is a
or b
or c
it doesn't matter.
I hope I'm clear. Any suggestion?
Any help would be very much appreciated.