I have some code that uses .ajax() to query a MySQL database to return some data which is then used by JavaScript code to "do stuff." Currently, my AJAX calls are set to "async : false," which makes the call "synchronous." I'd like some advice on how to change these synchronous calls to work asynchronously using callbacks by way of the .done() function, etc.
I will paste some code snippets that hopefully will show enough of what happens currently:
function lkup(data) {
param = data;
var tbl = param.tbl;
var sdate = param.sdate;
var called_by = param.called_by;
var hn2_event = param.hn2_event;
var field_id = param.field_id;
var code = param.code;
var field_to_populate = param.field_to_populate;
screen_data = param.screen_data;
var os_data = screen_data.os_data;
var osdc = screen_data.osdc;
var os_dmid = screen_data.os_dmid;
var os_dtnid = screen_data.os_dtnid;
var os_dln = screen_data.os_dln;
var os_dlnt = screen_data.os_dlnt;
var os_icode = screen_data.os_inv_code;
var os_dfid = screen_data.os_dfid;
var os_adj = screen_data.os_adj;
// Get the reco number for this reco:
var split_array = field_id.split("_n_");
var reco_number = split_array[1];
// Rebuild field_id:
field_id = split_array[0] + '_n_' + reco_number;
// Get various drug_tbl information for this code:
var drug_record_info='';
var drug_type='';
var ndc_needed='';
var disp_unit='';
// If this is a blank drug code, the user cleared out the drug. Do NOT try to get drug type:
if ( osdc == "" && (dc.test(field_id) || vx.test(field_id)) ) {
// do nothing
} else if (ic.test(field_id)) {
// do nothing
} else {
drug_record_info = getDrugType(osdc,drug_record_info);
var drug_info_array = drug_record_info.split("|");
drug_type = drug_info_array[0];
ndc_needed = drug_info_array[1];
disp_unit = drug_info_array[2];
}
// If the code was blank (''), clear the appropriate display-only fields:
// TODO: First, we have to figure out WHICH field is being populated; then, we need to handle clearing
// display-only fields
if (code == "") {
clear_dose_amount(reco_number, drug_type, ndc_needed);
clear_qty_on_hand(reco_number, drug_type, ndc_needed);
clear_unit_display(reco_number, tbl);
clear_dosage_form_id(reco_number);
}
.
.
.
In the snippet above, I'm interested in how to change the call to getDrugType so that it uses a callback function. Here's the getDrugType function:
//function getDrugType(code, drug_record_info) {
function getDrugType(code, drug_record_info) {
// Don't lookup anything if code is 'blank' or '':
if (code == '') {
return;
}
$.ajax({
type : "POST",
url : "HNJQUERYBRIDGE.pl",
cache : false,
async : false, // I need the javascript to wait for this function to complete!
data : {
dbase : param.dbase,
code : code,
session : param.session_id,
event : 'lookup_drug_type',
},
dataType : "text",
error : networkErrorMessage,
success : function(data) {
drug_record_info = setDrugType(data, drug_record_info);
},
});
return(drug_record_info);
}; // getDrugType()
//function setDrugType(data, drug_record_info) {
function setDrugType(data, drug_record_info) {
drug_record_info=data;
return(drug_record_info);
};
The data returned as "drug_record_info" is an array, which is used in various places throughout the rest of the JavaScript code as "drug_type" and "ndc_needed", etc.
If this code doesn't make sense I'll try to explain in more detail if necessary.
I appreciate any help!