I have a web application built with v3.5 of the JS API. I have some Jquery functionality that changes the map being built based on user input. My problem is that I can't get the legend to stay in sync with the map. On the first selection I build a map with new esri.Map and I create a new esri.dijit.Legend on the map's onLayersAddResult event. With the first selection everything works and is pretty snappy. When a user chooses the next map setup I've tried two different techniques:
- The first is to build a brand new map. I call map.destroy() on the old one to get rid of it. I create a new map and add my layers to it but a new legend does not get created on the onLayersAddResult. The event fires but it seems it cannot create a new legend. If i use legend.destroy() before creating a new one, it blows away the whole div and can't create the legend either.
- The second technique is not to create a new map. I remove all the layers from the map and add the new layers to the map. Then I point legend.map to the map and use legend.refresh() with the new layers as an argument. This works, sort of. It seems legend.refresh is really slow and if my second map has more than 3 or 4 layers in it (which it does) it will never draw. In fact it hijacks the whole app and everything is frozen.
So can anyone give me some tips on how to use the legend dijit correctly? Either get rid of it properly so I can build a new one or have it refresh correctly and promptly with new map layers?