Skip to content

Instantly share code, notes, and snippets.

@davidpiesse
Created July 8, 2018 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidpiesse/19060cf21f90f4a1460aa33954b4b957 to your computer and use it in GitHub Desktop.
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)
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")
}
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