Duplicate layers in TOC widget

1656
13
Jump to solution
03-21-2014 09:12 AM
CM2
by
New Contributor II
Hey folks,

I'm having an issue where layers I'm using in Nliu's TOC are showing up double in the TOC.

Check out my code on JSFiddle here: http://jsfiddle.net/749QG/1/

Weird! I'm not sure why it's doing that - does so no matter what service I use.

Thanks for any suggestions,

Craig
0 Kudos
1 Solution

Accepted Solutions
JonathanUihlein
Esri Regular Contributor
Hi! Thanks for the response. There are two errors with your current sample so let's fix those.

First, you are using parser.parse(); when you already have both "parseOnLoad: true" and "dojo/domReady!" in your code.
I noticed that you were copy/pasting code from Nliu's sample, which included the parser.

These can all be used in different situations. You may find value in reading documentation about the following modules:  "dojo/parser", "dom/ready", "dojo/domReady!". Also read about the 'parseOnLoad' djConfig property.

Secondly, you reference dom nodes that do not exist in your sample. Remove the following lines and your sample seems to work for me.

dojo.byId("ChangeFeatureRenderer").disabled = false; dojo.byId("SetVisibleLayersProgramatically").disabled = false; dojo.byId("FindNodeByLayer").disabled = false; dojo.byId("InsertNewLayer").disabled = false;


*edit,

You should always use try/catch blocks if you suspect a problem in a specific section of code.
I also recommend browser-based development tools for troubleshooting problems like these.

View solution in original post

13 Replies
JonathanUihlein
Esri Regular Contributor
It looks like you are creating a new TOC every time a layer is added to your map.

Your map has multiple layers being added, so the 'layer-add-result' is firing multiple times.
0 Kudos
CM2
by
New Contributor II
I don't see how that can be?

One layer is added to the TOC (once): 

layerInfos: [{
              layer: dynaLayer1,
              title: "McKenzie Parcels",
              //collapsed: true
              slider: true
              }]


and one layer is added to the map (once):

map.addLayers([dynaLayer1]);


How would I set it so that the TOC isn't firing more than it should?
0 Kudos
JonathanUihlein
Esri Regular Contributor
Hi! Thanks for the response. There are two errors with your current sample so let's fix those.

First, you are using parser.parse(); when you already have both "parseOnLoad: true" and "dojo/domReady!" in your code.
I noticed that you were copy/pasting code from Nliu's sample, which included the parser.

These can all be used in different situations. You may find value in reading documentation about the following modules:  "dojo/parser", "dom/ready", "dojo/domReady!". Also read about the 'parseOnLoad' djConfig property.

Secondly, you reference dom nodes that do not exist in your sample. Remove the following lines and your sample seems to work for me.

dojo.byId("ChangeFeatureRenderer").disabled = false; dojo.byId("SetVisibleLayersProgramatically").disabled = false; dojo.byId("FindNodeByLayer").disabled = false; dojo.byId("InsertNewLayer").disabled = false;


*edit,

You should always use try/catch blocks if you suspect a problem in a specific section of code.
I also recommend browser-based development tools for troubleshooting problems like these.
MichaelVolz
Esteemed Contributor
I am having the same issue when I use my own mapservice, so I cannot help out with this specific problem.

I do have some items to add about this TOC widget though.  The first instance of the layers in the TOC is not dynamic, so if you have layers with scale dependencies the top TOC never changes but the bottom TOC does so the top TOC is the problem.  The bottom TOC does not respect group layers as I have a group layer with scale dependencies, but the group layer never expands in the bottom TOC.
0 Kudos
BrettGreenfield__DNR_
Occasional Contributor II
The TOC is being loaded twice, for some reason.  I got rid of the entire toc.on section, and it only loaded once.  Strangely, I also got rid of toc.startup() and again, the TOC loaded but only once.  I'm not familiar with Nilu's TOC so I don't know exactly how it works, but if the things within the toc.on function aren't necessary, it looks like you can just get rid of those and the TOC will be fine.
0 Kudos
CM2
by
New Contributor II
Interesting;  thanks for the replies, everyone. Jon's removal of code worked for me. I thought I had tried that, but I must have removed too much.

Brett your suggestion worked for me as well.

Clearly I had been removing too much or too little when I was trying. Thanks a lot, guys. Is it obvious that I'm new to this? 😄

I'll certainly do some more reading. Thanks again. This is working:  http://jsfiddle.net/749QG/4/
0 Kudos
MayJeff
Occasional Contributor
I am so new to JavaScript API and try to change version to 3.9 on this sample:
http://jsfiddle.net/749QG/4/ but found error message on console when using Firefox.  Can someone try to explain it?
Thanks.
0 Kudos
KenBuja
MVP Esteemed Contributor
You have both

        var djConfig = {
            parseOnLoad: true,
            packages: [{
                "name": "agsjs",
                "location": "http://gmaps-utility-gis.googlecode.com/svn/tags/agsjs/latest/build/agsjs"
            }]
        };


and

parser.parse();


Use one or the other. And take a look at this page on parseOnLoad vs. parser.parse()
0 Kudos
MayJeff
Occasional Contributor
Thank you for your suggestion and the info page.  But I still see the error message.
http://jsfiddle.net/xZ5QQ/1/
0 Kudos