This is most likely something I'm overlooking but I can't figure out exactly what the issue is in this situation.
Upload: function (callback) {
var formData, xhr, file,
xhrLoaded = function () {
if (callback) {
callback(this.status, this.response, file);
}
};
if (this._files.length < 1) {
return false;
}
file = this._files.pop();
while (file) {
formData = new FormData();
formData.append("file", file);
xhr = new XMLHttpRequest();
xhr.open("POST", this.options.uploadUrl);
xhr.onload = xhrLoaded;
xhr.send(formData);
file = this._files.pop();
}
}
Why is it the case that when the xhr onload event handler runs file is undefined? At first I thought it was because onload is getting called out of the current scope, but that didn't seem right based on my understanding of javascript scoping. So as a test I checked this on a simple page.
(function () {
var test = "This is a test", testFunc = function () { console.log(test); };
window.onload = testFunc;
})();
and the variable test has the correct value. This tells me it has something to do with the file variable changing. So tried to add var localFile = file
to the xhrLoaded
method to see if it would keep the correct value in that case but it doesn't appear to be the case.