I'm new to the module pattern and closures in javascript. I'm trying to create a module that gets JSON data so I can display it on my webpage.
var EmailLogs = (function(){
var emailLogs = [];
var config = {};
function init(options){
for(var prop in options) {
if(options.hasOwnProperty(prop)){
config[prop] = options[prop];
}
}
setEmailLogs();
}
function setEmailLogs(){
$.ajax({
type: "POST",
headers : { "cache-control": "no-cache" },
url: "../../ajax/adminGetEmailLogs.php",
data: {options: JSON.stringify(config)},
dataType: 'json',
success: function(values){
if(values.success)
{
emailLogs = values.emailLogs;
}
}
});
}
function getEmailLogs(){
return emailLogs;
}
return{
init: init,
getEmailLogs: getEmailLogs,
}
})();
var options = {
sData : [
'email_id'
],
filters : {
user_id : 44860,
}
}
EmailLogs.init(options);
console.log(EmailLogs.getEmailLogs());
I'm trying to run the ajax call when init is run. Then I'm looking to get the emailLogs variable to display. I presume because my ajax is being run async, that's why I can't get my variables after. How do I make sure setEmailLogs() has ran before I running getEmailLogs().