I want to create an object with global scope which has data loaded from an external JSON file. I want to use the data in this object once the document has loaded. i.e not wait for user input. I am trying to use the jquery differed method. I assume that the Jquery get method generates the appropriate differed object for me to use later. The code for declaring the object is below
var myData = {
init: function () {
$.getJSON('data/data.json', function (raw) {
myData.data = raw;
alert(myData.data['UWLO']['name']);
yes = true;
$.each(raw, function (code, details) {
if (yes){
alert(code);
}
yes = false;
nameList[details.name] = code;
});
//alert(nameList);
});
},
data: {},
nameList: {},
findByCode: function (code) {
return myData.data[code];
},
getCode: function (name) {
return nameList[name];
},
getNameStrings: function () {
return Object.keys(nameList);
}
}
The code as it appears to initialise the object is
$(document).ready(function () {
$.when(myData.init()).then(function () {
alert(myData.nameList);
});
});
The alert(myData.data['UWLO']['name']); request gives the correct value so the json file is loaded correctly and of the correct format the alert(code); line works so the script correctly enters the loop I think the problem lies with generating nameList