Select to view content in your preferred language

Bug with dojo/on is version 3.7

3328
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
1 Solution

Accepted Solutions
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());

View solution in original post

0 Kudos
24 Replies
MattLane
Frequent Contributor
Should it not be
map.on("layers-add-result", function(e) {
...


?
0 Kudos
ShawnHolyoak
Deactivated User
Not according to the dojo/on documentation, https://dojotoolkit.org/reference-guide/1.9/dojo/on.html
0 Kudos
JonathanUihlein
Esri Regular Contributor
Both are valid.

map.on("layers-add-result", function(e) {
//stuff
});

on(map, "layers-add-result", function(e) {
//stuff
});


In that document holyoaks linked, there is another document linked at the bottom that discusses both uses.
https://dojotoolkit.org/reference-guide/1.9/dojo/Evented.html#dojo-evented

Map is an object that fires its own events, as explained here:
https://developers.arcgis.com/en/javascript/jsapi/map-amd.html

Now, onto troubleshooting...
Does the event fire?
What happens when you wrap the problematic code in a try/catch?
0 Kudos
ShawnHolyoak
Deactivated User
Both are valid.

map.on("layers-add-result", function(e) {
//stuff
});

on(map, "layers-add-result", function(e) {
//stuff
});



Does the event fire?

What happens when you wrap the problematic code in a try/catch?


The error is caught, with the message originally mentioned, "Cannot read property 'tagName' of undefined".  map.on(), though, as suggested by mattlane86, also works properly.  So it seems it's the on(target, event, listener) style that is returning the error.
0 Kudos
JonathanUihlein
Esri Regular Contributor
Are you able to fire the event with a simple console.log in the function block, like in this jsfiddle?


http://jsfiddle.net/NcCWw/


*edit

Also, you should probably be creating the TOC object earlier on in your code (preferably before the parser runs). You can still start it up inside your event.
0 Kudos
JeffPace
MVP Alum
i have noticed similar issues with layers add

map.on works, on(map style does not
0 Kudos
ShawnHolyoak
Deactivated User
Are you able to fire the event with a simple console.log in the function block, like in this jsfiddle?


http://jsfiddle.net/NcCWw/


*edit

Also, you should probably be creating the TOC object earlier on in your code (preferably before the parser runs). You can still start it up inside your event.


No.  The event isn't fired.  The error seems to be in attaching the event handler to the map.
0 Kudos
ShawnHolyoak
Deactivated User
i have noticed similar issues with layers add

map.on works, on(map style does not


How do I file a bug report?
0 Kudos
JonathanUihlein
Esri Regular Contributor
No.  The event isn't fired.  The error seems to be in attaching the event to the map.


Hmmm... could you replicate the issue with http://jsfiddle.net/ so I can take a look at your code?
0 Kudos