1 /*/////////////////////////////////////////////////////////////////////////////2 //3 AJAX DEFINITION (BEGIN) //4 */5 /**6 * Description: Manage ajax connections with a queue model.7 * Author: gerardooscarjt@gmail.com8 * Date: 2013/31/019 * Typical use:10 11 var ajax = new Ajax();12 ajax.setUrl('http://example.com/path');13 ajax.setMethod('post');14 ajax.setData({'name':'a','age':'22'};15 ajax.callback.done = function(xhr) {16 // ...17 };18 ajax.callback.error = function(xhr) {19 // ...20 };21 ajax.send();22 23 */24 var Ajax = function() {25 26 Ajax.prototype.requests.push(this);27 28 this._id = Ajax.prototype.id_generator;29 Ajax.prototype.id_generator++;30 31 this.callback = {32 done:null,33 error:null,34 progress:null,35 presend:null36 };37 38 39 40 this._method = 'get';41 this._async = true;42 this._url = '';43 this._data = null;44 this._retries = 3;45 this._xhr = null;46 47 }48 49 Ajax.prototype.requests = [];50 Ajax.prototype.queue = [];51 Ajax.prototype.id_generator = 0;52 Ajax.prototype.concurrency_limit = 4;53 54 /// METHOD ///55 Ajax.prototype.setMethod = function(method) {56 if (!this.isQueued())57 this._method = method;58 };59 Ajax.prototype.getMethod = function(method) {60 return this._method;61 };62 63 /// URL ///64 Ajax.prototype.setUrl = function(url) {65 if (!this.isQueued())66 this._url = url;67 };68 Ajax.prototype.getUrl = function() {69 return this._url;70 };71 72 /// DATA ///73 Ajax.prototype.setData = function(data) {74 if (!this.isQueued())75 this._data = data;76 };77 Ajax.prototype.getData = function () {78 return this._data;79 };80 81 /// IS QUEUED ///82 Ajax.prototype.isQueued = function () {83 return Ajax.prototype.queue.indexOf(this) > -1;84 };85 86 Ajax.prototype.send = function() {87 if (!this.isQueued()) {88 logger.log(this._id + ' no está en la cola: encolando...');89 Ajax.prototype.queue.push(this);90 Ajax._move();91 } else {92 logger.log('ya está en la cola'); 93 }94 logger.show();95 };96 97 98 /// STATIC METHODS ///99 100 // Abort all current requests101 Ajax._abort_all = function() {102 alert('aborting all requests');103 };104 105 /* Move queue */106 Ajax._move = function() {107 108 if (window.navigator.onLine) {109 while (Ajax.prototype.concurrency_limit > 0 && Ajax.prototype.queue.length > 0) {110 var a = Ajax.prototype.queue.pop();111 a._send();112 logger.log('Desencolado '+a._id);113 Ajax.prototype.concurrency_limit--;114 }115 }116 };117 118 /// PRIVATE METHODS (I WOULD LIKE) ///119 Ajax.prototype._send = function() {120 var that = this;121 122 logger.log('Send request for '+this._id);123 124 this._xhr = new XMLHttpRequest({mozSystem:true});125 126 this._xhr.open(this._method, this._url, this._async);127 128 this._xhr.onreadystatechange = function(event) {129 if(that._xhr.readyState == XMLHttpRequest.DONE) {130 logger.log('DONE! ' + that._id +' (delay '+that._xhr.responseText+')');131 Ajax.prototype.concurrency_limit++;132 Ajax._move();133 }134 };135 136 this._xhr.onerror = function(event) {137 logger.log('ERROR! ' + that._id);138 Ajax.prototype.concurrency_limit++;139 Ajax._move();140 };141 142 this._xhr.send();143 144 145 };146 147 // Initialize onLine and offLine events148 window.addEventListener('online', function() {149 Ajax._move();150 }, true);151 152 window.addEventListener('offline', function() {153 Ajax._abort_all();154 }, true);155 156 /*157 AJAX DEFINITION (END) //158 //159 /////////////////////////////////////////////////////////////////////////////*/160 161 162 window.addEventListener('load', function(){163 //test1(); 164 }, true);165 166 167 /**168 * Launch 10 ajax calls169 */170 function test1() {171 172 logger.show();173 174 for (var i=0; i<10; i++) {175 var ajax = new Ajax();176 ajax.setUrl('http://www.treeweb.es/request');177 ajax.callback.done = function(xhr){178 logger.log('Completed '+ajax._id);179 };180 ajax.send();181 logger.log('Launched '+ajax._id);182 }183 184 logger.log(Ajax.prototype.queue);185 186 187 }188 189
Este ShareCode tiene versiones:
- God Ajax Test 1 See stats See chart ... (04/02/2013)
- God Ajax Test 1 ... (24/04/2013)
- God Ajax Test 1 ... (24/04/2013)
- God Ajax Test 1 ... (24/04/2013)
- God Ajax Test 1 ... (24/04/2013)
- God Ajax Test 1 ... (24/04/2013)
Enlace
El enlace para compartir es: