i'm trying make variable depend on callback ajax function, however; can't seem working. want make sure defaults.context has value before proceeding other code.
what doing wrong or how can achieve in proper way?
var defaults = { currentcase: undefined, context: {} } // set defaults function initdefaults(){ defaults.currentcase = getcurrentcase(); defaults.context = getcontext(defaults.currentcase, function(object){ console.log(object); // logs right data return object; }); console.log(defaults.context); // logs undefined } initdefaults(); // id of current case function getcurrentcase(){ return global_vars.project_id; } function getcontext(id, callback){ var obj = {}; $.get(global_vars.template_url + "/includes/load-project-context.php?id=" + id, function(data) { obj = json.parse(data); }).complete(function() { callback(obj); }); }
thanks in regards,
enzio
you can use like
global_vars.template_url + "/includes/load-project-context.php?id=" + id, function(data) { obj = json.parse(data); }).complete(function() { callback(obj); }).fail(function() { callback(error); });
this callback chaining. can use other callbacks handle other use cases.
Comments
Post a Comment