phaser/src/loader/XHRLoader.js

52 lines
1.6 KiB
JavaScript
Raw Normal View History

2016-11-30 00:18:34 +00:00
var MergeXHRSettings = require('./MergeXHRSettings');
/**
* Creates a new XMLHttpRequest (xhr) object based on the given File and XHRSettings
* and starts the download of it. It uses the Files own XHRSettings and merges them
* with the global XHRSettings object to set the xhr values before download.
*
* @function Phaser.Loader.XHRLoader
* @since 3.0.0
*
* @param {Phaser.Loader.File} file - The File to download.
* @param {Phaser.Loader.XHRSettings} globalXHRSettings - The global XHRSettings object.
*
* @return {XMLHttpRequest} The XHR object.
*/
2016-11-30 00:18:34 +00:00
var XHRLoader = function (file, globalXHRSettings)
{
var config = MergeXHRSettings(globalXHRSettings, file.xhrSettings);
2016-11-30 00:18:34 +00:00
var xhr = new XMLHttpRequest();
xhr.open('GET', file.src, config.async, config.user, config.password);
xhr.responseType = file.xhrSettings.responseType;
2016-11-30 00:18:34 +00:00
xhr.timeout = config.timeout;
if (config.header && config.headerValue)
{
xhr.setRequestHeader(config.header, config.headerValue);
}
if (config.overrideMimeType)
{
xhr.overrideMimeType(config.overrideMimeType);
}
// After a successful request, the xhr.response property will contain the requested data as a DOMString, ArrayBuffer, Blob, or Document (depending on what was set for responseType.)
xhr.onload = file.onLoad.bind(file);
xhr.onerror = file.onError.bind(file);
xhr.onprogress = file.onProgress.bind(file);
// This is the only standard method, the ones above are browser additions (maybe not universal?)
// xhr.onreadystatechange
2016-11-30 00:18:34 +00:00
xhr.send();
return xhr;
};
module.exports = XHRLoader;