Select to view content in your preferred language

TOC not loading legend

2411
5
Jump to solution
01-22-2016 06:47 AM
RichardMoussopo
Frequent Contributor

The toc widget is acting strange. it is not loading. sometimes after multiple refresh, it loads fine.

noLegendToc.PNG

here is the error message I get:

errorToc.PNG

here is my code

//define Proxy
    esriConfig.defaults.io.proxyUrl = "proxy.ashx";
    esriConfig.defaults.io.alwaysUseProxy = false;
     //============================
    map.on("load", function (evt) {
        mapReady();
       getLayers();
    });

  //LEGEND CHECKBOX
    function getLayers() {      
            map.on("layer-add-result", function (evt) {
                // console.log(evt);
                try {
                    toc = new TOC({
                        map: map,
                        layerInfos: [{
                            layer: layerName,
                            title: "Layers",
                            collapsed: false,
                            slider: true
                        }]
                    }, 'legendDiv');
                    toc.startup();
                }
                catch (ex) {
                    // console(ex.toString());
                }
            }) 
        }

I am not sure why the toc is not loading correctly. Any ideas?

The error message is :

message: "Unable to load http://www.arcgis.com/sharing/tools/legend?soapUrl=http

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

One addition...when using the "layers-add-result" event, you'll have to add the layers using the syntax

map.addLayers([yourLayer1, yourLayer2]);

View solution in original post

5 Replies
AdrianWelsh
MVP Honored Contributor

So it sometimes loads after multiple refreshes? Have you tried other browsers to see what happens? Have you tried clearing your cache on your current browser? It seems weird that it sometimes loads and sometimes doesn't. I am just curious to know what things you have tried in terms of troubleshooting.

0 Kudos
RichardMoussopo
Frequent Contributor

Yes, Adrian, after clearing all the caches and browser history, the toc does load the legend just fine. I don't understand what's going on the cache and the TOC. I going to try Ken idea.. and see

Thanks

0 Kudos
KenBuja
MVP Esteemed Contributor

You should start the TOC in the "layers-add-results" event because the "layer-add-result" event will fire for the basemap layers in addition to the other layers you add.

Add this line into your event to see how often it fires:

console.log("Added layer " + evt.layer.id);

KenBuja
MVP Esteemed Contributor

One addition...when using the "layers-add-result" event, you'll have to add the layers using the syntax

map.addLayers([yourLayer1, yourLayer2]);

RichardMoussopo
Frequent Contributor

Ken is absolutely right!

that fixed the issue.

Thank you.

0 Kudos