Select to view content in your preferred language

Bug with dojo/on is version 3.7

3318
24
Jump to solution
10-02-2013 07:06 AM
ShawnHolyoak
Deactivated User
In upgrading my application to version 3.7 of the api, I've run into what I believe is a bug.  When I try to connect to the layers-add-result event for my map, I try to use the dojo/on amd method.  My code for that is:
on(map, "layers-add-result", function(e) {   var toc = new agsjs.dijit.TOC({    map: map,    layerInfos:legendLayers   }, 'legendDiv');   toc.startup();  });


That returns an error "TypeError: Cannot read property 'tagName' of undefined ".  When I try using the old dojo.connect method, though, everything works.  My code using the old dojo.connect is:
dojo.connect(map,'onLayersAddResult', function(results){   var toc = new agsjs.dijit.TOC({    map: map,    layerInfos:legendLayers   }, 'legendDiv');   toc.startup();  });


According to the documents, version 3.7 is using dojo version 1.9.1, which should certainly support the dojo/on functionality.  What, if anything, am I doing wrong.  If I'm not doing anything wrong, how do I file a bug report?
0 Kudos
24 Replies
ShawnHolyoak
Deactivated User
Hmmm... could you replicate the issue with http://jsfiddle.net/ so I can take a look at your code?


Hmm.  I can't replicate it http://jsfiddle.net/NcCWw/7/.  It's working in this instance.  Just not working with my full code.  I suppose it's possible that it's the TOC widget I'm using that's causing the issue.
0 Kudos
JonathanUihlein
Esri Regular Contributor
Hmm.  I can't replicate it http://jsfiddle.net/NcCWw/7/.  It's working in this instance.  Just not working with my full code.  I suppose it's possible that it's the TOC widget I'm using that's causing the issue.


OK great! We've narrowed it down.
I would try initializing the TOC before my ready and parser calls, to make sure it's accessible when the page first loads.
0 Kudos
ShawnHolyoak
Deactivated User
OK great! We've narrowed it down.
I would try initializing the TOC before my ready and parser calls, to make sure it's accessible when the page first loads.


I don't think I can do that.  The TOC requires the map and an array of layers for its constructor, and since the map requires the divId to be created, I have to do that after domReady!, no?
0 Kudos
JeffPace
MVP Alum
I:

1. create map
2. add layers
3. create toc on the "layers-add" event
this.map = new Map("map", {                                     infoWindow: popup,                                     basemap: "streets",                                     autoResize: true,                                     sliderStyle: style,                                     logo: false,                                     showAttribution: false,                                     lods: lods                                 });                                 map = this.map;     var tocAdder = map.on('layers-add-result', lang.hitch(this, function(results) {                                     // alert('in onlayeradd');                                     if (!this.toc) {                                         this.toc = new agsjs.dijit.TOC({                                             map: map,                                             layerInfos: layerInfos.reverse()                                         }, 'tocDiv');                                          toc = this.toc;                                         toc.startup();                                          lang.hitch(this, createLegend());
0 Kudos
JonathanUihlein
Esri Regular Contributor
+1 to Jeff; he beat me to it!

@Holyoaks, when you find the final solution to your problem, make sure you mark this thread as answered.
Many people use nliu's TOC widget (it's great!) and this is a useful thread in case anyone runs into a similar problem.
0 Kudos
JeffPace
MVP Alum
here's my version of it

http://www.mymanatee.org/gisapps/mapviewer/mobile/mobiletest.html

Warning: site in development

I made a couple small tweaks to it, and added functionality of loading layers from a set of json files.

You can change the layers that are currently loaded on the "settings" tab
0 Kudos
TracySchloss
Honored Contributor
I'm having an issue today with something similar, but my code is slightly older, 3.4.  I have made no changes at all to this code for over a week and it was working as of yesterday.  I have a dojo.connect on the map for onLayersAddResult, which is used to create the toc.

      map.addLayers([eezLayer, eezFeatureLayer]);  

  dojo.connect(map, 'onLayersAddResult', function(results) {
        var toc = new agsjs.dijit.TOC({
          map: map,
          layerInfos: [{
             layer: eezLayer,
            title: "Enhanced Enterprise Zones",
            slider:true 
          },{
            layer: districtLayer,
            title: "Legislative Districts"
          }]
        }, 'tocDiv');
        toc.startup();  
      });


This never executes because it never detects the onLayersAddResult event has fired.  I'm completely baffled.  This code has been in test for a month and today it suddenly decided not to work?! 

The layers do get added, but I have no TOC because of this.
0 Kudos
JohnGravois
Deactivated User
since the map class itself is evented, i'm not sure I understand the need to try and use 'dojo/on' to handle event wiring.

@tracy.  also not sure whats causing your problem.
0 Kudos
TracySchloss
Honored Contributor
That turned out to be operator error.  I had changed around the location of one of my services yesterday to be in the subfolder where it belonged.  Since the layer wasn't really valid, then onLayersAddResult never fired. 

I thought I'd changed all the places I used the layer, but I missed one.   I'm developing my own mental list of Murphy's Laws.  If you are 100% certain you've identified the line of code where your problem lies, there is a 100% certainty your problem lies elsewhere.
0 Kudos
ShawnHolyoak
Deactivated User
since the map class itself is evented, i'm not sure I understand the need to try and use 'dojo/on' to handle event wiring.

@tracy.  also not sure whats causing your problem.


Because esri says it uses dojo 1.9.1 which allows it, but more importantly, not all esri objects seem to be evented (e.g., the navigation toolbar).
0 Kudos