3

I am building a map application, calling layers from an arcgis-server using the javascript api.

Maybe I am being stubborn, but I am trying to minimize my use of dojo throughout this process. Much of my previous experience has been using other APIs like cartoDb, google maps and I prefer using jquery and pure javaScript.

I am going to build a dynamic list of layers and legend items by iterating through the layerInfos of the dynamicMapServiceLayer that I have added to my map.

I have found that I cannot itterate through the layerInfos until after the layer has been added to the map. To detect this I am using the onLayerAdd event. The only way I've been able to use the onLayerAdd event is to also use a dojo.connect

dojo.connect(map, "onLayerAdd",testFunction);

Do I have to use dojo.connect? If so why? It seems like it would make more sense to do something like

map.onLayerAdd(testFunction)

Thanks in advance....

3
  • Are you opposed to using jQuery? You can just use the plain javascript "addEventListner" on the object to listen to when a layer is added. Only other solution is to check if the layers are added first, then do what you need too. An event listener is much easier.
    – Jamie
    Mar 6, 2013 at 17:00
  • Also, the ESRI javasscript API depends and is built using Dojo so that is why most examples use Dojo.
    – Jamie
    Mar 6, 2013 at 17:01
  • 1
    as of version 3.5 of the JS API map, layers and tasks support 'on' style events. in version 3.6 support for 'on' style events was added for all other classes. developers.arcgis.com/en/javascript/jshelp/new_v35.html Sep 12, 2013 at 22:06

1 Answer 1

3

Have you looked at this sample of using jquery with arcgis javascript api?

It says "When building applications with frameworks other than Dojo, you still need to use dojo to connect to map events. In this snippet, we connect to the map's onLoad event and then call .ready().The .ready() method is automatically called when the DOM is fully loaded."

dojo.connect(map, "onLoad", function() {
    console.log("Map onLoad event");
    $(document).ready(jQueryReady);
});
dojo.connect(map, "onLayerAdd", function() {
    console.log("Map onLayerAdd event");
});

http://help.arcgis.com/en/webapi/javascript/arcgis/jssamples/#sample/framework_jquery

I can understand wanting to use another framework for different reasons, but as @Jamie says the arcgis api is built with dojo and is already loaded. There is no performance penalty at that point. You can also use the compact build as mentioned in the above sample which exludes dijits and would be a smaller download.

2
  • thanks awesomo. good point about using dojo since it is already loaded.
    – jotamon
    Mar 6, 2013 at 17:33
  • I think it maybe possible to use JQuery bind to listen to the events, but I have not tried it yet.
    – Jamie
    Mar 6, 2013 at 20:55

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.