-
Notifications
You must be signed in to change notification settings - Fork 0
/
pithy.ajax.js
70 lines (70 loc) · 2.32 KB
/
pithy.ajax.js
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
/*
* Simple javascript AJAX library.
* Copyright Sandro Magi 2020
* */
!function(_){
"use strict";
//if (this[_]) throw new Error(_ + ' is already defined.');
var e = Element.prototype,
$ = window[_] = document,
lg = console.log;
$.uriPart = encodeURIComponent;
$.url = function(o, pre) {
var q = [];
for (var p in o) {
if (o.hasOwnProperty(p)) {
var v = o[p],
k = pre ? (isNaN(parseInt(p)) ? pre + '.' + p : pre + '[' + p + ']') : p;
if (v !== null && typeof v === 'object'){
q.push($.url(v, k));
} else {
q.push($.uriPart(k));
q.push('=');
q.push($.uriPart(v));
q.push('&');
}
}
}
return q.join('');
};
$.ajax = function(m,u,a,l,p) {
var r = new XMLHttpRequest(),
t = [], v = [], d = [],
c = null;
r.open(m,u,a||true,l,p);
function app(q, r, e){
for(var i=0; i < q.length; ++i)
try { q[i](r, e); } catch(e) {lg(e);}
}
r.on('load', function(e){
if (c)
r.responseType = c;
app(t, r, e);
app(d, r, e);
});
function err(e){
app(v, r, e);
app(d, r, e);
}
r.on('error', err);
r.on('abort', err);
return Object.create({
on: function(e,f) { r.on(e,f); return this; },
off: function(e,f) { r.on(e,f); return this; },
abort: r.abort.bind(r),
header: function(k,v) { r.setRequestHeader(k,v); return this; },
as: function(m){ c = m; return this; },
timeout: function(t) { r.timeout=t; return this; },
ok: function(f) { t.push(f); return this; },
fail: function(f) { v.push(f); return this; },
send: function(b) { r.send(b); return this; },
dispose: function(f) { d.push(f); return this; }
});
};
// $.ajax("GET", "http://sdfsdfs", true, "smagi@...", "foo")
// .timeout(100)
// .on('load', function(e) {})
// .result(function(r,e){})
// .catch(function(r,e){})
// .send();
}(this.exports || '$');