I'm working on learning OO Javascript, callbacks and closures and I'm a bit baffled by this behavior. Here is my working code, the callback works and I get the response in the console:
var XMLLoader = (function() {
console.log("Successfully loaded 'XMLLoader.js'...");
var xml;
var xhr;
return {
setXml: function(xmlPath) {
xml = xmlPath;
},
makeRequest: function(callback) {
xhr = new XMLHttpRequest();
xhr.onload = function() {
callback(xhr.responseXML);
};
xhr.open("GET", xml);
xhr.send(null);
}
};
}());
XMLLoader.setXml("xml/test.xml");
XMLLoader.makeRequest(function(e) {
console.log(e);
});
The issue that I'm having is that I would like to store the responseXML inside the XMLLoader and return it using something like var x = XMLLoader.getXMLDoc();
Can anyone point me in the right direction on how to do this? I've tried making assignments from within the callback to no avail.