Readability of nested AJAX callback functions

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?

以上就是Readability of nested AJAX callback functions的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏