Trying to figure out the logic behind a question but coming up empty. In my JavaScript app using OOP, I'm attempting to assign a function as a variable value.
Initially, the value of init_s.__temp_var
is an empty object. When the function logic_s.get_active_statuses(..)
is called, it should overwrite this value. However, that's not happening. Here's what's going on:
var widget = {
settings: {
__temp_var: {}
},
init: function () {
init_s = this.settings;
logic_s = this.functions_available;
},
functions_available: {
get_active_statuses: function (id) {
jQuery.ajax({
url: .....,
type: 'POST',
dataType: 'JSON',
data: {
....
},
success: function (data) {
init_s.__temp_var = data; // not working
return data; // undefined
},
error: function (e) {
console.log(e.message);
}
});
}
},
my_actions: {
logic: function () {
var active_services_status = logic_s.get_active_statuses(5);
console.log(init_s.__temp_var); // should print {id : 5}
console.log(active_services_status ); // if used with return prints "undefined"
}
}
};
When I call logic_s.get_active_statuses(5);
for the first time, it logs an empty object. Then, when called for the second time, it logs {id : 5}
. This pattern continues, with the returned value increasing by increments of 5 every fourth call. Why is this behavior occurring? It appears that the variable is being set after it is printed.
EDIT:
the current behavior causing the "undefined" return:
function get_active_status (id) {
jQuery.ajax({
url: .....,
type: 'POST',
dataType: 'JSON',
data: {
....
},
success: function (data) {
return data; // undefined
},
error: function (e) {
console.log(e.message);
}
});
};
get_active_status(5); // returns "undefined" instead of the sent object