While working on a JavaScript program, I encountered an issue with losing data stored in an object literal after making an ajax call within a certain scope.
I'm confused about what is causing this problem.
var menusView = {
menusRep: null,
init: function () {
this.menusRep = menusRepository;
this.getMenus();
},
getMenus: function () {
$.ajax({
url: 'data/voordeelmenus.json',
dataType: 'json',
success: function (data) {
menusView.menusRep.menus = data;
console.log(data);//console output: [object, object,...]
console.log(menusView.menusRep.menus);//console output: [object, object,...]
},
error: function (error) {
alert("error reading file: " + error);
}
});
console.log(menusView.menusRep.menus); //console output: []
}
}
var menusRepository = {
menus: []
}
I believe I have provided all the relevant code for this issue. Any help would be greatly appreciated!