ajax: do not clutter global

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

@ -20,146 +20,147 @@ function ajax(options) {
} else { } else {
return parseResponse(xhr, options); return parseResponse(xhr, options);
} }
}
ajax.defaults = { function applyDefaults(options) {
async: true, for (var key in ajax.defaults) {
dataType: "json", if (options[key] == null) {
timeout: 30, options[key] = ajax.defaults[key];
type: "GET", }
};
// contentType
// context
// data
// error
// headers
// progress
// success
// url
var applyDefaults = function (options) {
for (var key in ajax.defaults) {
if (options[key] == null) {
options[key] = ajax.defaults[key];
} }
} }
};
var serializeData = function (options) { function serializeData(options) {
if (!options.data) { if (!options.data) {
return; return;
} }
if (options.type === "GET") { if (options.type === "GET") {
options.url += "?" + serializeParams(options.data); options.url += "?" + serializeParams(options.data);
options.data = null; options.data = null;
} 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) {
if (!options.async) {
return;
} }
xhr.timer = setTimeout( function applyCallbacks(xhr, options) {
onTimeout.bind(undefined, xhr, options), if (!options.async) {
options.timeout * 1000, return;
);
if (options.progress) {
xhr.onprogress = options.progress;
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
clearTimeout(xhr.timer);
onComplete(xhr, options);
} }
};
};
var applyHeaders = function (xhr, options) { xhr.timer = setTimeout(
if (!options.headers) { onTimeout.bind(undefined, xhr, options),
options.headers = {}; options.timeout * 1000,
);
if (options.progress) {
xhr.onprogress = options.progress;
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
clearTimeout(xhr.timer);
onComplete(xhr, options);
}
};
} }
if (options.contentType) { function applyHeaders(xhr, options) {
options.headers["Content-Type"] = options.contentType; if (!options.headers) {
} options.headers = {};
}
if ( if (options.contentType) {
!options.headers["Content-Type"] && options.headers["Content-Type"] = options.contentType;
options.data && }
options.type !== "GET"
) {
options.headers["Content-Type"] = "application/x-www-form-urlencoded";
}
if (options.dataType) { if (
options.headers["Accept"] = !options.headers["Content-Type"] &&
MIME_TYPES[options.dataType] || options.dataType; options.data &&
} options.type !== "GET"
) {
options.headers["Content-Type"] = "application/x-www-form-urlencoded";
}
if (options.dataType) {
options.headers["Accept"] =
MIME_TYPES[options.dataType] || options.dataType;
}
for (var key in options.headers) { for (var key in options.headers) {
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) {
onSuccess(response, xhr, options); onSuccess(response, xhr, options);
} else {
onError("invalid", xhr, options);
}
} else { } else {
onError("invalid", xhr, options); onError("error", xhr, options);
} }
} else {
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