Please help me to refactore this Javascript code. There is big form for scheduled message sending(send date, reccurence type, end by date/qauntity, credits system - need to count total cost of scheduled sending plan in runtime). I'm writing Javascript validator for this form.
There is an validation algorithm 1) check if send date time is not in past moment 2) check if "end by date" field time is greater then first send date time 3) validate total cost of schedule plan
(There about 6 steps, but I just write here 3 of them - I think it will be enough to grasp the problem)
"Save scheduled plan" button has a javascript listener on "click" event. This listener calls this function:
ScheduledValidator.checkIfSendDateTimeIsNotInPast(params, form);
Here is it its declaration:
ScheduledValidator.checkIfSendDateTimeIsNotInPast = function (params, form) {
var conn = new Ext.data.Connection();
conn.request({
url: CONST.BASE_URL + 'url',
params: params,
callback: function (options, success, response) {
response = Ext.util.JSON.decode(response.responseText);
if (response.success == false) {
// display error messages
} else {
ScheduledValidator.checkIfEndDateIsGreaterThatSendDate(params, form);
}
}
});
}
We have nested request later:
ScheduledValidator.checkIfEndDateIsGreaterThatSendDate = function (params, form) {
var conn = new Ext.data.Connection();
conn.request({
url: CONST.BASE_URL + 'url2',
params: params,
messageForm: form,
callback: function (options, success, response) {
response = Ext.util.JSON.decode(response.responseText);
if (response.success == false) {
// display error messages
} else {
ScheduledValidator.validateTotalCost(params, form);
}
}
});
}
and one more here:
ScheduledValidator.validateTotalCost = function (params, form) {
...
I don't like in that approach, that it is quite hard to understand the algorithm at first glance. Maybe it is not good, to make many(about 6) nested AJAX queries for validation of single form? Maybe it should be merged to the single request and after that we will do all the validation activities at server side? How should I refactor this code?