Created
July 8, 2018 18:33
-
-
Save davidpiesse/19060cf21f90f4a1460aa33954b4b957 to your computer and use it in GitHub Desktop.
A auto loader that allows for loading modules quickly with an await function and then call anywhere else (Allows you to get out the the UMD function)
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
import esri from '../moduleLoader' | |
async loaded(){ | |
await esri.loadModules([ | |
'esri/Map', | |
'esri/views/MapView', | |
'esri/widgets/Compass', | |
],true) | |
} | |
//Somewhere else after loaded() is called | |
addCompass(){ | |
let compass = new Compass({ | |
view: this.map_view, | |
}) | |
this.map_view.ui.add(compass, "top-left") | |
} |
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
import esriLoader from 'esri-loader' | |
import _ from 'lodash' | |
export default{ | |
async loadModules(modules, attach_to_window = true){ | |
let _modules = await this.load(modules) | |
_.forEach(_modules, (module,key) => { | |
if(attach_to_window){ | |
window[key] = module | |
} | |
Object.defineProperty(this, key, { | |
get: () => { | |
return module | |
} | |
}) | |
}) | |
}, | |
preLoad(){ | |
esriLoader.loadScript() | |
}, | |
load(modules) { | |
let module_classes = _.map(modules, (module) => { | |
//Special case for using the Map keyword as a object | |
if(_.last(_.split(module, '/')) == 'Map'){ | |
return 'EsriMap' | |
} | |
return _.last(_.split(module, '/')) | |
}) | |
let class_names = module_classes | |
return new Promise(resolve => { | |
esriLoader.loadModules(modules).then((module_classes) => { | |
let zip = _.zipObject(class_names,module_classes) | |
resolve(zip) | |
}) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment