Select to view content in your preferred language

How to use require js Sugar Syntax with ESRI JSAPI

3508
3
08-24-2015 02:53 PM
BrianBehling
Deactivated User

I'd like to use the Require.js Sugar Syntax for loading JSAPI modules, i.e.:

define(function (require) {
   var map= require('esri/map'),

  
//do something with map
});

but when I try to instantiate a new Map object, an error is thrown that says "undefinedModule".

How can we make this work?

Message was edited by: Brian Behling

0 Kudos
3 Replies
ReneRubalcava
Esri Frequent Contributor

Dojo doesn't yet support this. Right now, it needs to be

define(function (require) {
  require(['esri/map'], function(Map) {

         //do something with map

  });
});

This lazy loads the map. That being said, I did see some discussion at one point not long ago in Dojo IRC that this would be implemented, but I think it's going to be in Dojo2 as I don't see commit history for it in Dojo 1.x. As of right now, this only works if you have previously already loaded the module asynchronously somewhere else in your application.

0 Kudos
BrianBehling
Deactivated User

That's disappointing. It would be nice to use the sugar syntax when you have to load many ESRI modules. It gets quite messy using the standard syntax.

Maybe in JSAPI 4.0, ESRI will just use require.js and drop the Dojo loaders?

0 Kudos
ReneRubalcava
Esri Frequent Contributor

There will be some stuff coming up soon that will allow expanded RequireJS support. Dojo is not going anywhere, but the ability to use RequireJS loader will be easier. RequireJS has some limitations when it comes to cross-domain loading of files that Dojo can handle.

0 Kudos