7

When using RequireJS to load a JavaScript library from a CDN, an example syntax is shown at https://github.com/requirejs/example-jquery-cdn:

requirejs.config({
    "paths": {
      "jquery": "//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min"
    }
});

How should the ArcGIS Server JS API be referenced? The page at https://developers.arcgis.com/javascript/ gives the syntax for a standard web page:

<script src="http://js.arcgis.com/3.12/"></script>

but when using RequireJS:

requirejs.config({
    'paths': {
      'arcgis': '//js.arcgis.com/3.12/'
    }
});

this returns NetworkError: 404 Not Found - http://js.arcgis.com/3.12/.js

What is the correct syntax for referencing the ArcGIS Server JS API when using RequireJS?

2
  • 3
    Last time I tried this it was a bad idea - ArcGIS JS API includes Dojo, and Dojo provides its own AMD functionality which conflicts with RequireJS. Immediately on load it threw an error. Unless the situation has changed recently you will have to use Dojo's version of define and require. Generally my experience of having to use Dojo because of the JS API's reliance on it has been bad.
    – tomfumb
    Dec 29, 2014 at 16:25
  • @tomfumb thanks for the tip. Using Aamir's suggestion below seems to have done the trick Jan 1, 2015 at 10:18

2 Answers 2

7

You would do this

requirejs.config({
  'paths': {
    'esri': 'http://js.arcgis.com/3.12/esri',
    'dojo': 'http://js.arcgis.com/3.12/dojo',
    'dojox': 'http://js.arcgis.com/3.12/dojox',
    'dijit': 'http://js.arcgis.com/3.12/dijit'
  }
});

Then require your modules in AMD style like this

requirejs(["esri/dijit/BasemapGallery", 
         "dojo/dom", 
         "dijit/form/Button", 
         "dojox/grid/DataGrid"], 
         function(BasemapGallery, dom, Button, DataGrid){
});
0

Actually I found out that when I load ArcGIS API to my HTML then I can use require like requireJS (but with '.js' at the end)

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <!--The viewport meta tag is used to improve the presentation and behavior of the samples 
          on iOS devices-->
        <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
        <link rel="stylesheet" href="http://js.arcgis.com/3.14/esri/css/esri.css">
        <link rel="stylesheet" href="css/custom_style.css">
        <script src="http://js.arcgis.com/3.14/"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script src="app.js"></script>
        <script>
require([
    'dojo/parser',
    'test.js',
    'dijit/layout/BorderContainer', 'dijit/layout/ContentPane',
    'dojo/domReady!'
], function(parser){
    parser.parse();
        ...
});
        </script>
    </head>
    <body class="claro">
        ...
    </body>
</html>

And test.js is a javascript file I have on my localhost and it executes.

1
  • Thanks for the tip. But this question is actually about how to load the ArcGIS JS API (http://js.arcgis.com/3.14/) itself, not the test.js file Jul 19, 2015 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.