esri-loader
Version:
A tiny library to help load ArcGIS API for JavaScript modules in non-Dojo applications
44 lines (43 loc) • 1.9 kB
JavaScript
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
import { getScript, isLoaded, loadScript } from './script';
import utils from './utils/index';
// wrap Dojo's require() in a promise
function requireModules(modules) {
return new utils.Promise(function (resolve, reject) {
// If something goes wrong loading the esri/dojo scripts, reject with the error.
var errorHandler = window['require'].on('error', reject);
window['require'](modules, function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
// remove error handler
errorHandler.remove();
// Resolve with the parameters from dojo require as an array.
resolve(args);
});
});
}
// returns a promise that resolves with an array of the required modules
// also will attempt to lazy load the ArcGIS API if it has not already been loaded
export function loadModules(modules, loadScriptOptions) {
if (loadScriptOptions === void 0) { loadScriptOptions = {}; }
if (!isLoaded()) {
// script is not yet loaded, is it in the process of loading?
var script = getScript();
var src = script && script.getAttribute('src');
if (!loadScriptOptions.url && src) {
// script is still loading and user did not specify a URL
// in this case we want to default to the URL that's being loaded
// instead of defaulting to the latest 4.x URL
loadScriptOptions.url = src;
}
// attempt to load the script then load the modules
return loadScript(loadScriptOptions).then(function () { return requireModules(modules); });
}
else {
// script is already loaded, just load the modules
return requireModules(modules);
}
}