Appcan Development Notes: Combined with JQuery's $. Deferred (), appcanjquery completes batch asynchronous transmission.
Appcan uexXmlHttpMgr. send or appcan. ajax cannot synchronize requests (this attribute is not found), and can only be asynchronous. As a result, the response cannot be synchronized due to delays or network congestion during multiple rounds of submission, resulting in disordered submission order, callback errors or data loss after execution, such as the traditional method (JQ package referenced here)
1 var data=[]; 2 var d=[1,2,3,4,5,6]; 3 $.each(d, function(i, v) { 4 var req = uexXmlHttpMgr.create({ 5 method : "GET", 6 url :myurl 7 }) 8 uexXmlHttpMgr.send(req, 0, function(status, resStr, resCode, resInfo) { 9 if (status == 1) {10 data.push(i+"OK");11 }12 });13 });14 alert(JSON.stringify(data))
The output result is []. Because multiple sending replies are called, The each and request are not executed when data is output. Therefore, data must be of no value. $. Deferred () is referenced in this example.
1 var dtd = $. Deferred (); // create a new Deferred object 2 var wait = function (dtd) {3 var tasks = function () {4 alert ("execution completed! "); 5 dtd. resolve (); // change the execution status of the Deferred object 6}; 7 setTimeout (tasks, 5000); 8 return dtd; 9}; 10 $. when (wait (dtd) 11. done (function () {alert ("Haha, success! ") ;}) 12. fail (function () {alert (" error! ");});
I will not describe too much here. The usage of $. Deferred () is described as follows:
Http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html
Http://api.jquery.com/category/deferred-object/combine appcanrequest to get the following code
1 var d = [1, 2, 3, 4, 5, 6]; 2 var data = []; 3 var sendalldata = function () {4 var dtdall = $. deferred (); 5 $. each (d, function (I, v) {6 var io = I; 7 var req = uexXmlHttpMgr. create ({8 method: "GET", 9 url: myurl10}) 11 uexXmlHttpMgr. send (req, 0, function (status, resStr, resCode, resInfo) {12 if (status = 1) {13 console. log ("result" + I + ":" + resStr); 14 resStr = eval ('+ resStr +'); 15 data. push (resStr. toString () 16 if (d. length = count) {17 dtdall. resolve (JSON. stringify (data); 18} 19} 20}); 21}) 22 return dtdall. promise (); 23} 24 $. when (sendalldata ()). done (function (v1) {25 console. log (v1) 26 v1 = eval ('+ v1 +'); 27 console. log ("result v1" + ":" + JSON. stringify (v1); 28 });
The output is as expected (the running environment is ID4.0 ). There are two other solutions. The first one is the infinite loop, which is common on the Internet. However, if there is a complicated callback code, it will be messy. In the above Code, if (d. length = count ){... in fact, it also means an infinite loop. The second method is to use the js native method promise, but the appcan kernel has not reached ES6 (Array. prototype. filter and Array. prototype. ), so this method is abandoned.