-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (146 loc) · 3.73 KB
/
index.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
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
import connect from 'can-connect';
import param from 'can-param';
import cookies from 'js-cookie';
/* A stripped down version from can-util/dom/ajax is here, with support for tastypie and csrftoken
*/
// from https://gist.github.com/mythz/1334560
var xhrs = [
function () { return new XMLHttpRequest(); },
function () { return new ActiveXObject("Microsoft.XMLHTTP"); },
function () { return new ActiveXObject("MSXML2.XMLHTTP.3.0"); },
function () { return new ActiveXObject("MSXML2.XMLHTTP"); }
],
_xhrf = null;
var makeXhr = function () {
if (_xhrf != null) {
return _xhrf();
}
for (var i = 0, l = xhrs.length; i < l; i++) {
try {
var f = xhrs[i], req = f();
if (req != null) {
_xhrf = f;
return req;
}
} catch (e) {
continue;
}
}
return function () { };
};
var _xhrResp = function (xhr, options) {
switch (options.dataType || xhr.getResponseHeader("Content-Type").split(";")[0]) {
case "text/xml":
case "xml":
return xhr.responseXML;
case "text/json":
case "application/json":
case "text/javascript":
case "application/javascript":
case "application/x-javascript":
case "json":
return JSON.parse(xhr.responseText);
default:
return xhr.responseText;
}
};
// used to check for sameOrigin requests
function _sameOrigin(url) {
var loc = window.location,
a = document.createElement('a');
a.href = url;
return a.hostname == loc.hostname &&
a.port == loc.port &&
a.protocol == loc.protocol;
}
var ajax = function (o) {
var xhr = makeXhr();
var deferred = {};
var sameOrigin = _sameOrigin(o.url);
var promise = new Promise(function(resolve,reject){
deferred.resolve = resolve;
deferred.reject = reject;
});
xhr.onreadystatechange = function () {
try {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300) {
deferred.resolve( _xhrResp(xhr, o) );
} else {
deferred.reject( xhr );
}
}
} catch(e) {
deferred.reject(e);
}
};
var url = o.url, data = null, type = o.type.toUpperCase();
var isPost = type === "POST" || type === "PUT" || type === "PATCH";
if (!isPost && o.data) {
url += "?" + param(o.data);
}
xhr.open(type, url);
if (isPost) {
data = sameOrigin ?
(typeof o.data === "object" ? JSON.stringify(o.data) : o.data):
param(o.data);
// CORS simple: `Content-Type` has to be `application/x-www-form-urlencoded`:
xhr.setRequestHeader("Content-Type", sameOrigin ? "application/json" : "application/x-www-form-urlencoded");
if (sameOrigin) {
xhr.setRequestHeader("X-CSRFToken", cookies.get('csrftoken'));
}
}
// CORS simple: no custom headers, so we don't add `X-Requested-With` header:
if (sameOrigin){
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
}
xhr.send(data);
return promise;
};
export default connect.behavior('data/tastypie', function(baseConnection) {
var behavior = {
_instance_uri: function(instance) {
return instance[this.resource_uri_field || 'resource_uri'];
},
_parse_listdata: function(result) {
return result[this.listdata_field || 'objects'];
},
getListData: function(params) {
var self = this;
return ajax({
type: 'GET',
url: this.endpoint,
data: params
}).then(function(result) {
return self._parse_listdata(result);
});
},
getData: function(params) {
return ajax({
type: 'GET',
url: this.endpoint + this.id(params) + '/'
});
},
createData: function(params) {
return ajax({
type: 'POST',
url: this.endpoint,
data: params
});
},
updateData: function(params) {
return ajax({
type: 'PUT',
url: this._instance_uri(params),
data: params
});
},
destroyData: function(instance) {
return ajax({
type: 'DELETE',
url: this._instance_uri(params)
});
}
};
return behavior;
});