Skip to content

Instantly share code, notes, and snippets.

@jwasilgeo
Last active February 24, 2016 21:41
Show Gist options
  • Save jwasilgeo/cb3d8a6cdae0d7b891a5 to your computer and use it in GitHub Desktop.
Save jwasilgeo/cb3d8a6cdae0d7b891a5 to your computer and use it in GitHub Desktop.
convert an array of Dojo AMD module paths and loaded modules to a deeply nested object
function createModulesObject(modulePaths, modules) {
const targetObject = {};
modulePaths.forEach(function(modulePathString, idx) {
_createNestedProperties(modulePathString.split('/'), targetObject, modules[idx]);
});
return targetObject;
}
function _createNestedProperties(modulePathArray, targetObject, moduleToAssign) {
const modulePathPart = modulePathArray.shift();
if (modulePathArray.length) {
if (!targetObject.hasOwnProperty(modulePathPart)) {
targetObject[modulePathPart] = {};
}
_createNestedProperties(modulePathArray, targetObject[modulePathPart], moduleToAssign);
} else {
targetObject[modulePathPart] = moduleToAssign;
}
}
const modulePaths = [
'esri/Map',
'esri/views/MapView',
'esri/widgets/Home/HomeViewModel',
'dojo/dom'
];
// dojo require
require(modulePaths, function(...modules) {
const mappedModules = createModulesObject(modulePaths, modules);
// const map = new mappedModules.esri.Map(...);
// mappedModules.dojo.dom.byId(...)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment