Dynamically load ArcGIS online modules

616
2
Jump to solution
08-10-2019 10:07 AM
WilliamFerster
New Contributor

It is possible to dynamically decide which models to require()?

It takes a while to load a bunch of widgets, using the JavaScript require() function, so I want to be able to only include the ones I'm using in any given instance to the ones I'm using.

Currently, I do this:

var reqs="esri/Map""esri/WebMap", ... "esri/widgets/Legend";
require(reqs, function(Map, WebMap, ... ,Legend,) { 
...
It's easy enough to fill the reqs array, but how can I vary the names in the function?
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Have you considered loading the modules only when you need them? In her blog posting about code snippets, Raluca Nicola‌ uses this syntax:

const view = require("esri/views/View").views.getItemAt(0);

View solution in original post

2 Replies
WilliamFerster
New Contributor

Came up with a solution. Not as elegant as one would like (i.e. having to spec the class names directly), but it works.

var reqs="esri/Map", "esri/WebMap", ... "esri/widgets/Legend";  
require(app.reqs, function()
      var Map, WebMap, MapView, SceneView, KMLLayer,  ...  Legend;
      for (i=0;i<app.reqs.length;++i) {
      key=app.reqs[i].match(/([^\/]+)$/i)[1]; 
      if (key == "Map")          Map=arguments[i];
      else if (key == "WebMap")  WebMap=arguments[i];
        ...
     else if (key == "Legend")  Legend=arguments[i];
     }
0 Kudos
KenBuja
MVP Esteemed Contributor

Have you considered loading the modules only when you need them? In her blog posting about code snippets, Raluca Nicola‌ uses this syntax:

const view = require("esri/views/View").views.getItemAt(0);