code structure correctness

608
3
Jump to solution
05-26-2014 11:41 PM
RalucaNicola
New Contributor
Hi,

I have 2 questions related to the structure of my code:

1. I define a map and a dynamic layer and then I add a function that will be executed when both the layer and the map are loaded. In the beginning I added an event listener to the map that adds an event listener to the layer that executes the function. Is that correct? My problem is that the function initializeThings sometimes gets executed, sometimes doesn't. I haven't figured out a pattern for when it does and when not, but I assume it has something to do with these event listeners. In the end I set the event listener just on the layer, but still sometimes the function doesn't get executed.

2. Is it correct to "unnest" my functions from this main function(Map, DynamicLayer )? At the moment all my functions are inside this main function and I am not sure if that is the best practice.

require (["esri/map", "esri/layers/ArcGISTiledMapServiceLayer", "dojo/domReady!"],     function (Map, DynamicLayer ) {      var map = new Map("mapCanvas", options);        var layer = new DynamicLayer("http://....");    function initializeThings() {     }      //map.on("load", function (){layer.on("load", function() {initializeThings();} )} );   layer.on("load", function() {inititializeThings();} ); })
0 Kudos
1 Solution

Accepted Solutions
JohnGravois
Frequent Contributor
hi Raluca,

1. its definitely okay to nest one event listener inside another

2. if you are planning to reference esri js api modules (like 'Map', 'DynamicLayer') in your named functions, they will no longer be in scope if the function is defined entirely outside of your require callback. 

that being said, you are welcome to call require more than once in your own code if you prefer.

hope that helps!

View solution in original post

0 Kudos
3 Replies
TimWitt
Frequent Contributor
Raluca,

1) just write it like this:

map.on("load", function () {
#What you want happen when the map is loaded
});


This function will run once when the map is loaded.

You would follow the same steps for your layers.

2) Once everything you want to happen in your map is done just close it with )};

Hope this makes sense.

Tim
0 Kudos
RalucaNicola
New Contributor
Hi Tim,

Thanks for your answer!
My problem is that I want the layer to be loaded too when I run the initialize function. So do you think that adding event listeners like this is correct?
map.on("load", function (){
         layer.on("load", function() {initialize();} )
         } );


My second question is if it would be ok to write the initialize function outside of this main function that I use to define the map and the layers..then the map and the layers should be global variables?..something like this:

require (["esri/map", "esri/layers/ArcGISTiledMapServiceLayer", "dojo/domReady!"], 
 
 function (Map, DynamicLayer ) {
  
  map = new Map("mapCanvas", options);   
         map.on("load", function(){initialize()})
  layer = new DynamicLayer("http://....");

})

function initialize () {
//do something with the layer
}

The reason why I am asking this is because I have a lot of functions that I create and I don't want to nest them all inside of this require() function. But I am not really sure how it is better.

Thanks again!
Raluca
0 Kudos
JohnGravois
Frequent Contributor
hi Raluca,

1. its definitely okay to nest one event listener inside another

2. if you are planning to reference esri js api modules (like 'Map', 'DynamicLayer') in your named functions, they will no longer be in scope if the function is defined entirely outside of your require callback. 

that being said, you are welcome to call require more than once in your own code if you prefer.

hope that helps!
0 Kudos