javascript - Looking for pattern to chain AJAX calls -


we have ajax web application, , common pattern have number of ajax calls call 1 after other. common when save multiple attached entities. example might savecustomer() --> savecustomeraddress() --> savecustomersorder().

we have when methoda() succeeds, ie first method, calls methodb(), , forth (see code below). disadvantage pattern is hard see going on. if read methoda() have no idea reading method name calls methodb() , methodc(). other disadvantage if want change chaining have rewrite lot of code, , if want call method individually, can't done because call methods downstream.

function tester() {     this.url = 'https://public.opencpu.org/ocpu/library/';      this.save = function() {         this.methoda();     }      this.methoda = function () {         var self = this;          $.ajax({             url: self.url,             async: true         }).always(function (processeddataorxhrwrapper, textstatus, xhrwrapperorerrorthrown) {              //check errors... , if ok             alert('a ok');             self.methodb();          })     }     this.methodb = function () {         var self = this;          $.ajax({             url: self.url,             async: true         }).always(function (processeddataorxhrwrapper, textstatus, xhrwrapperorerrorthrown) {              //check errors... , if ok             alert('b ok');             self.methodc();          })     }     this.methodc = function () {         var self = this;          $.ajax({             url: self.url,             async: true         }).always(function (processeddataorxhrwrapper, textstatus, xhrwrapperorerrorthrown) {             //ok             alert('c ok');         })     } } new tester().save(); 

i scratching head trying figure out better pattern. figured wrap later methods in callback functions, , somehow pass them through each method i'm not sure how approach this.

is aware of common type of pattern can remove method dependencies when chaining methods?

function a() {     writemessage("calling function a");     return $.ajax({         url: "/scripts/s9/1.json",         type: "get",                             datatype: "json"     }); }   function b(resultfroma) {     writemessage("in function b. result = " + resultfroma.data);     return $.ajax({         url: "/scripts/s9/2.json",         type: "get",         datatype: "json"     }); }   function c(resultfromb) {     writemessage("in function c. result b =  " + resultfromb.data);     return $.ajax({         url: "/scripts/s9/3.json",         type: "get",         datatype: "json"     }); }  function d(resultfromc) {     writemessage("in function d. result c = " + resultfromc.data); }  a().then(b).then(c).then(d);  function writemessage(msg) {     $("#para").append(msg + "</br>");                  } 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -