ajax.count¶
webix.ajax.
count
([see official doc])¶webix.ajax.count helper.
Please look into the linked official documentation.
References¶
- helpers
assert()
,assert_error()
,extend()
,isArray()
,log()
,log_full_time()
.
External references¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | webix.ajax.count = 0;
webix.ajax.prototype={
master:null,
//creates xmlHTTP object
getXHR:function(){
return new XMLHttpRequest();
},
stringify:function(obj){
var origin = Date.prototype.toJSON;
Date.prototype.toJSON = function(){
return webix.i18n.parseFormatStr(this);
};
var result;
if (obj instanceof Date)
result = obj.toJSON();
else
result = JSON.stringify(obj);
Date.prototype.toJSON = origin;
return result;
},
/*
send data to the server
params - hash of properties which will be added to the url
call - callback, can be an array of functions
*/
_send:function(url, params, call, mode){
var master;
if (params && (webix.isArray(params) || (typeof (params.success || params.error || params) == "function"))){
master = call;
call = params;
params = null;
}
var defer = webix.promise.defer();
var x=this.getXHR();
if (!webix.isArray(call))
call = [call];
call.push({ success: function(t, d){ defer.resolve(d); },
error: function(t, d){ defer.reject(x); }});
var headers = this._header || {};
if (!webix.callEvent("onBeforeAjax", [mode, url, params, x, headers, null, defer])) return;
//add content-type to POST|PUT|DELETE
var json_mode = false;
if (mode !== 'GET'){
var found = false;
for (var key in headers)
if (key.toString().toLowerCase() == "content-type"){
found = true;
if (headers[key] == "application/json")
json_mode = true;
}
if (!found)
headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
//add extra params to the url
if (typeof params == "object" && !(params instanceof window.FormData)){
if (json_mode)
params = this.stringify(params);
else {
var t=[];
for (var a in params){
var value = params[a];
if (value === null || value === webix.undefined)
value = "";
if(typeof value==="object")
value = this.stringify(value);
t.push(a+"="+encodeURIComponent(value));// utf-8 escaping
}
params=t.join("&");
}
}
if (params && mode==='GET'){
url=url+(url.indexOf("?")!=-1 ? "&" : "?")+params;
params = null;
}
x.open(mode, url, !this._sync);
var type = this._response;
if (type) x.responseType = type;
//if header was provided - use it
for (var key in headers)
x.setRequestHeader(key, headers[key]);
//async mode, define loading callback
var self=this;
this.master = this.master || master;
x.onreadystatechange = function(){
if (!x.readyState || x.readyState == 4){
if (webix.debug_time) webix.log_full_time("data_loading"); //log rendering time
webix.ajax.count++;
if (call && self && !x.aborted){
//IE8 and IE9, handling .abort call
if (webix._xhr_aborted.find(x) != -1)
return webix._xhr_aborted.remove(x);
var owner = self.master||self;
var is_error = x.status >= 400 || x.status === 0;
var text, data;
if (x.responseType == "blob" || x.responseType == "arraybuffer"){
text = "";
data = x.response;
} else {
text = x.responseText||"";
data = self._data(x);
}
webix.ajax.$callback(owner, call, text, data, x, is_error);
}
if (self) self.master=null;
call=self=master=null; //anti-leak
}
};
if (this._timeout)
x.timeout = this._timeout;
//IE can use sync mode sometimes, fix it
if (!this._sync)
setTimeout(function(){
if (!x.aborted){
//abort handling in IE9
if (webix._xhr_aborted.find(x) != -1)
webix._xhr_aborted.remove(x);
else
x.send(params||null);
}
}, 1);
else
x.send(params||null);
if (this.master && this.master._ajax_queue)
this.master._ajax_queue.push(x);
return this._sync?x:defer; //return XHR, which can be used in case of sync. mode
},
_data:function(x){
return {
xml:function(){
try{
return webix.DataDriver.xml.tagToObject(webix.DataDriver.xml.toObject(x.responseText, this));
}
catch(e){
webix.log(x.responseText);
webix.log(e.toString()); webix.assert_error("Invalid xml data for parsing");
}
},
rawxml:function(){
if (!window.XPathResult)
return webix.DataDriver.xml.fromString(x.responseText);
return x.responseXML;
},
text:function(){ return x.responseText; },
json:function(){
return webix.DataDriver.json.toObject(x.responseText, false);
}
};
},
//GET request
get:function(url,params,call){
return this._send(url,params,call,"GET");
},
//POST request
post:function(url,params,call){
return this._send(url,params,call,"POST");
},
//PUT request
put:function(url,params,call){
return this._send(url,params,call,"PUT");
},
//DELETE request
del:function(url,params,call){
return this._send(url,params,call,"DELETE");
},
//PATCH request
patch:function(url,params,call){
return this._send(url,params,call,"PATCH");
},
sync:function(){
this._sync = true;
return this;
},
timeout:function(num){
this._timeout = num;
return this;
},
response:function(value){
this._response = value;
return this;
},
//deprecated, remove in 3.0
//[DEPRECATED]
header:function(header){
webix.assert(false, "ajax.header is deprecated in favor of ajax.headers");
this._header = header;
return this;
},
headers:function(header){
this._header = webix.extend(this._header||{},header);
return this;
},
bind:function(master){
this.master = master;
return this;
}
};
webix.ajax.$callback = function(owner, call, text, data, x, is_error){
if (owner.$destructed) return;
if (x === -1 && data && typeof data.json == "function")
data = data.json();
if (is_error)
webix.callEvent("onAjaxError", [x]);
if (!webix.isArray(call))
call = [call];
if (!is_error)
for (var i=0; i < call.length; i++){
if (call[i]){
var before = call[i].before;
if (before)
before.call(owner, text, data, x);
}
}
for (var i=0; i < call.length; i++) //there can be multiple callbacks
if (call[i]){
var method = (call[i].success||call[i]);
if (is_error)
method = call[i].error;
if (method && method.call)
method.call(owner,text,data,x);
}
};
/*submits values*/
|