-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbackbone.nativeajax.js
148 lines (126 loc) · 4.78 KB
/
backbone.nativeajax.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
// Backbone.NativeAjax.js 0.4.4
// ---------------
// (c) 2016 Adam Krebs, Paul Miller, Exoskeleton Project
// Backbone.NativeAjax may be freely distributed under the MIT license.
// For all details and documentation:
// https://github.com/akre54/Backbone.NativeAjax
(function (factory) {
if (typeof define === 'function' && define.amd) { define(factory);
} else if (typeof exports === 'object') { module.exports = factory();
} else { Backbone.ajax = factory(); }
}(function() {
// Make an AJAX request to the server.
// Usage:
// var req = Backbone.ajax({url: 'url', type: 'PATCH', data: 'data'});
// req.then(..., ...) // if Promise is set
var ajax = (function() {
var xmlRe = /^(?:application|text)\/xml/;
var jsonRe = /^application\/json/;
var getData = function(accepts, xhr) {
if (accepts == null) accepts = xhr.getResponseHeader('content-type');
if (xmlRe.test(accepts)) {
return xhr.responseXML;
} else if (jsonRe.test(accepts) && xhr.responseText !== '') {
return JSON.parse(xhr.responseText);
} else {
return xhr.responseText;
}
};
var isValid = function(xhr) {
return (xhr.status >= 200 && xhr.status < 300) ||
(xhr.status === 304) ||
(xhr.status === 0 && window.location.protocol === 'file:')
};
var end = function(xhr, options, promise, resolve, reject) {
return function() {
proxyPromise(xhr, promise);
if (xhr.readyState !== 4) return;
var status = xhr.status;
var data = getData(options.headers && options.headers.Accept, xhr);
// Check for validity.
if (isValid(xhr)) {
if (options.success) options.success(data);
if (resolve) resolve(data);
} else {
var error = new Error('Server responded with a status of ' + status);
if (options.error) options.error(xhr, status, error);
if (reject) reject(xhr);
}
}
};
var proxyPromise = function(xhr, promise) {
if (!promise) return;
var props = ['readyState', 'status', 'statusText', 'responseText',
'responseXML', 'setRequestHeader', 'getAllResponseHeaders',
'getResponseHeader', 'statusCode', 'abort'];
for (var i = 0; i < props.length; i++) {
var prop = props[i];
try {
promise[prop] = typeof xhr[prop] === 'function' ?
xhr[prop].bind(xhr) :
xhr[prop];
} catch (e) {
console.log(e);
}
}
return promise;
}
return function(options) {
if (options == null) throw new Error('You must provide options');
if (options.type == null) options.type = 'GET';
var resolve, reject, xhr = new XMLHttpRequest();
var PromiseFn = ajax.Promise || (typeof Promise !== 'undefined' && Promise);
var promise = PromiseFn && new PromiseFn(function(res, rej) {
resolve = res;
reject = rej;
});
if (options.contentType) {
if (options.headers == null) options.headers = {};
options.headers['Content-Type'] = options.contentType;
}
// Stringify GET query params.
if (options.type === 'GET' && typeof options.data === 'object') {
var query = '';
var stringifyKeyValuePair = function(key, value) {
return value == null ? '' :
'&' + encodeURIComponent(key) +
'=' + encodeURIComponent(value);
};
for (var key in options.data) {
query += stringifyKeyValuePair(key, options.data[key]);
}
if (query) {
var sep = (options.url.indexOf('?') === -1) ? '?' : '&';
options.url += sep + query.substring(1);
}
}
xhr.onreadystatechange = end(xhr, options, promise, resolve, reject);
xhr.open(options.type, options.url, options.async !== false);
if(!(options.headers && options.headers.Accept)) {
var allTypes = "*/".concat("*");
var xhrAccepts = {
"*": allTypes,
text: "text/plain",
html: "text/html",
xml: "application/xml, text/xml",
json: "application/json, text/javascript"
};
xhr.setRequestHeader(
"Accept",
options.dataType && xhrAccepts[options.dataType] ?
xhrAccepts[options.dataType] + (options.dataType !== "*" ? ", " + allTypes + "; q=0.01" : "" ) :
xhrAccepts["*"]
);
}
if (options.headers) for (var key in options.headers) {
xhr.setRequestHeader(key, options.headers[key]);
}
if (options.beforeSend) options.beforeSend(xhr);
xhr.send(options.data);
options.originalXhr = xhr;
proxyPromise(xhr, promise);
return promise ? promise : xhr;
};
})();
return ajax;
}));