dojo addonload transition to AMD

1145
5
Jump to solution
10-01-2014 07:53 AM
GeorgeHilton__GISP
New Contributor III

I have a question on transitioning to AMD references and calling an initial function when an application loads.

Currently my pre AMD application contains the following line in my default.aspx:

<script type="text/javascript">

      dojo.addOnLoad(init);

  </script>

the init function is in a separate load.js file included in my visual studio web application.  At the top of the load.js file I list several requirements, example:

dojo.require("esri.map");

dojo.require("esri.layers.graphics");

then the init function begins which adds layers to the map.

I am trying to rewrite my code with AMD references like this:

require(["esri/Map", "esri/geometry/Extent", "dojo/ready",

                    "dojo/domReady!"], function (Map, Extent, registry, ready) {

                        ready(function () {

                           var initExtent = new Extent({ "xmin": -8227830.3637063, "ymin": 5340700.39573648, "xmax": -8171280.30528049, "ymax": 5394550.12077208, "spatialReference": { "wkid": 102100 } });

                            map = new Map("map", { extent: initExtent, slider: false, logo: false });

                            var qbydynamLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://toqgis.queensbury.net/ArcGIS/rest/services/webapp3/MapServer", {

                                "opacity": 0.5,

                                   });

                                 map.addLayer(qbydynamLayer);

                        })

                    })

I placed this code in the load.js file to replace the init function and it is not working.  I'm sure it's an easy fix, but I cannot seem to find a way to call this code from default.aspx with code similar to the old   dojo.addOnLoad reference.

Any help is appreciated.  Thanks in advance.

0 Kudos
1 Solution

Accepted Solutions
TimWitt2
MVP Alum

George,

you can get rid of "dojo/ready" and ready. However you need to add "dijit/registry" after "esri/geometry/Extent"

View solution in original post

0 Kudos
5 Replies
TimWitt2
MVP Alum

George,

you can get rid of "dojo/ready" and ready. However you need to add "dijit/registry" after "esri/geometry/Extent"

0 Kudos
TimWitt2
MVP Alum

Also check out this article: The abc’s of AMD | ArcGIS Blog

This should help you in transitioning.

0 Kudos
GeorgeHilton__GISP
New Contributor III

Thank you for your quick reply and the link to the ArcGIS Blog post.  I replaced items as you listed and I'm still having issues.  I am hung up on how to call this code when default.aspx begins loading. 

This was my code in default.aspx that called the init function:

<script type="text/javascript">

        dojo.ready(init);

   </script>

I'm not sure how to get the new code to fire from default.aspx as I was able to with the code above.

0 Kudos
JohnGravois
Frequent Contributor

as tim said, you don't need to call dojo.ready(init) anymore.  simply placing a <script> tag referencing your .js file in an .html or .aspx page while trigger the require and its callback firing.  see this example in the Resource Center.

in the js file itself, the code from

parser.parse();

...//to

var gs = new GeometryService("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer");

fires immediately after all the AMD modules have been loaded successfully.

to get a better idea of whats triggered when, i usually like to set breakpoints in the developer tools and step through manually.

0 Kudos
GeorgeHilton__GISP
New Contributor III

I finally got it to work.  I did remove dojo.ready(init), however the trick for me was to add my map service (the operational layer) in a separate function as displayed in this sample:

esri-jsapi-samples/fl_ondemand_AMD.html at gh-pages · jgravois/esri-jsapi-samples · GitHub

Thank you Tim and John for your help

0 Kudos