ajax: do not clutter global

pull/1441/head
Simon Legner 1 year ago
parent 228090ad83
commit eea754d19a

@ -20,32 +20,16 @@ function ajax(options) {
} else { } else {
return parseResponse(xhr, options); return parseResponse(xhr, options);
} }
}
ajax.defaults = { function applyDefaults(options) {
async: true,
dataType: "json",
timeout: 30,
type: "GET",
};
// contentType
// context
// data
// error
// headers
// progress
// success
// url
var applyDefaults = function (options) {
for (var key in ajax.defaults) { for (var key in ajax.defaults) {
if (options[key] == null) { if (options[key] == null) {
options[key] = ajax.defaults[key]; options[key] = ajax.defaults[key];
} }
} }
}; }
var serializeData = function (options) { function serializeData(options) {
if (!options.data) { if (!options.data) {
return; return;
} }
@ -56,17 +40,18 @@ var serializeData = function (options) {
} else { } else {
options.data = serializeParams(options.data); options.data = serializeParams(options.data);
} }
}; }
var serializeParams = (params) => function serializeParams(params) {
Object.entries(params) return Object.entries(params)
.map( .map(
([key, value]) => ([key, value]) =>
`${encodeURIComponent(key)}=${encodeURIComponent(value)}`, `${encodeURIComponent(key)}=${encodeURIComponent(value)}`,
) )
.join("&"); .join("&");
}
var applyCallbacks = function (xhr, options) { function applyCallbacks(xhr, options) {
if (!options.async) { if (!options.async) {
return; return;
} }
@ -84,9 +69,9 @@ var applyCallbacks = function (xhr, options) {
onComplete(xhr, options); onComplete(xhr, options);
} }
}; };
}; }
var applyHeaders = function (xhr, options) { function applyHeaders(xhr, options) {
if (!options.headers) { if (!options.headers) {
options.headers = {}; options.headers = {};
} }
@ -112,9 +97,9 @@ var applyHeaders = function (xhr, options) {
var value = options.headers[key]; var value = options.headers[key];
xhr.setRequestHeader(key, value); xhr.setRequestHeader(key, value);
} }
}; }
var onComplete = function (xhr, options) { function onComplete(xhr, options) {
if (200 <= xhr.status && xhr.status < 300) { if (200 <= xhr.status && xhr.status < 300) {
let response; let response;
if ((response = parseResponse(xhr, options)) != null) { if ((response = parseResponse(xhr, options)) != null) {
@ -125,41 +110,57 @@ var onComplete = function (xhr, options) {
} else { } else {
onError("error", xhr, options); onError("error", xhr, options);
} }
}; }
var onSuccess = function (response, xhr, options) { function onSuccess(response, xhr, options) {
if (options.success != null) { if (options.success != null) {
options.success.call(options.context, response, xhr, options); options.success.call(options.context, response, xhr, options);
} }
}; }
var onError = function (type, xhr, options) { function onError(type, xhr, options) {
if (options.error != null) { if (options.error != null) {
options.error.call(options.context, type, xhr, options); options.error.call(options.context, type, xhr, options);
} }
}; }
var onTimeout = function (xhr, options) { function onTimeout(xhr, options) {
xhr.abort(); xhr.abort();
onError("timeout", xhr, options); onError("timeout", xhr, options);
}; }
var abort = function (xhr) { function abort(xhr) {
clearTimeout(xhr.timer); clearTimeout(xhr.timer);
xhr.onreadystatechange = null; xhr.onreadystatechange = null;
xhr.abort(); xhr.abort();
}; }
var parseResponse = function (xhr, options) { function parseResponse(xhr, options) {
if (options.dataType === "json") { if (options.dataType === "json") {
return parseJSON(xhr.responseText); return parseJSON(xhr.responseText);
} else { } else {
return xhr.responseText; return xhr.responseText;
} }
}; }
var parseJSON = function (json) { function parseJSON(json) {
try { try {
return JSON.parse(json); return JSON.parse(json);
} catch (error) {} } catch (error) {}
}
}
ajax.defaults = {
async: true,
dataType: "json",
timeout: 30,
type: "GET",
// contentType
// context
// data
// error
// headers
// progress
// success
// url
}; };

Loading…
Cancel
Save