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._url = '';42 this._data = null;43 this._retries = 3;44 this._concurrency_limit45 }46 47 Ajax.prototype.requests = [];48 Ajax.prototype.queue = [];49 Ajax.prototype.id_generator = 0;50 51 /// METHOD ///52 Ajax.prototype.setMethod = function(method) {53 if (!this.isQueued())54 this._method = method;55 };56 Ajax.prototype.getMethod = function(method) {57 return this._method;58 };59 60 /// URL ///61 Ajax.prototype.setUrl = function(url) {62 if (!this.isQueued())63 this._url = url;64 };65 Ajax.prototype.getUrl = function() {66 return this._url;67 };68 69 /// DATA ///70 Ajax.prototype.setData = function(data) {71 if (!this.isQueued())72 this._data = data;73 };74 Ajax.prototype.getData = function () {75 return this._data;76 };77 78 /// IS QUEUED ///79 Ajax.prototype.isQueued = function () {80 return Ajax.prototype.queue.indexOf(this) > -1;81 };82 83 Ajax.prototype.send = function() {84 if (!this.isQueued()) {85 logger.log('no está en la cola: encolando...');86 Ajax.prototype.queue.push(this);87 Ajax._move();88 } else {89 logger.log('ya está en la cola'); 90 }91 logger.show();92 };93 94 95 /// STATIC METHODS ///96 97 // Abort all current requests98 Ajax._abort_all = function() {99 alert('aborting all requests');100 };101 102 /* Move queue */103 Ajax._move = function() {104 alert('Move move !'+Ajax.prototype.queue.length);105 };106 107 /// PRIVATE METHODS (I WOULD LIKE) ///108 Ajax.prototype._send = function() {109 alert('TODO: perform request');110 };111 112 // Initialize onLine and offLine events113 window.addEventListener('online', function() {114 Ajax._move();115 }, true);116 117 window.addEventListener('offline', function() {118 Ajax._abort_all();119 }, true);120 121 /*122 AJAX DEFINITION (END) //123 //124 /////////////////////////////////////////////////////////////////////////////*/125 126 127 window.addEventListener('load', function(){128 test1(); 129 }, true);130 131 132 /**133 * Launch 10 ajax calls134 */135 function test1() {136 137 var ajax = new Ajax();138 ajax.url = 'http://www.treeweb.es/request';139 ajax.callback.done = function(xhr){140 logger.log('');141 };142 ajax.send();143 ajax.send();144 ajax.send();145 146 }147 148 149 150 151 152
Este ShareCode tiene versiones:
- God Ajax ... (01/02/2013)
- God Ajax ... (24/04/2013)
- God Ajax ... (24/04/2013)
- God Ajax ... (24/04/2013)
- God Ajax ... (24/04/2013)
- God Ajax ... (24/04/2013)
Enlace
El enlace para compartir es: