Last active
February 24, 2016 21:41
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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