First of all sorry for my english,
I am working with jqgrid. I have a table for manage clients. When I want to add a new client I have an ajax validation in beforeSubmit event to check if client exists on a external system. The first parameter of this function is a boolean (if true, execution of adding a client continues, if false it stops)
beforeSubmit: function(postdata, formid) {
var form = formid[0];
var hostId = $.trim(form.HOSTID.value);
var document = $.trim(form.DOCUMENT.value);
var idMurex = $.trim(form.IDMUREX.value);
var success;
processClientData(hostId, document, idMurex).done(function() {
alert('OK');
success = true;
}).fail(function() {
alert('KO');
success = false;
});
return[success, ''];
},
The ajax function returns if client exists or not. If client exists it shows a confirmation dialog for continue or not depending if user is agree with the client information. If client doesn´t exist it show an alert dialog showing an error message.
function processClientData(hostId, document, idMurex) {
var def = $.Deferred();
//SERVER RESPONSE IF EXISTS CLIENT
$.post('/watt/cambio_titularidad', {
oper: 'datos_cliente',
hostId: hostId,
document: document,
idMurex: idMurex
},
function(response){
//CLIENT EXISTS (SHOW CONFIRMATION DIALOG WITH CLIENT INFO)
if (response.success == true) {
$("#dialog_info_tablas").dialog({
title: "CLIENT EXISTS",
modal: true,
buttons: {
"Ok": function() {
$(this).dialog("close");
def.resolve();
}
"Cancel": function() {
$(this).dialog("close");
def.reject();
}
}
});
//CLIENT DOESN´T EXIST (SHOW ALERT DIALOG WITH ERROR MESSAGE)
} else {
$("#dialog_info_tablas").dialog({
title: "CLIENT NOT EXISTS",
modal: true,
buttons: {
"Ok": function() {
$(this).dialog("close");
def.reject();
}
}
});
}
},
"json")
return def.promise();
}
I use Deferred object to try to synchronize ajax call.
The issue is that return statement is executed before var success has a correct value. I don´t know how to make it synchronous, I need to delay the return until user click on dialog buttons, thus to continue adding client or not. I tried to put the return statements inside done and fail functions but it didn´t work neither
Can anyone help me??
Thanks in advance