Select to view content in your preferred language

Is esri.dijit.Legend.destroy() destroying everything?

931
2
10-05-2012 01:32 PM
JoanneMcGraw
Frequent Contributor
In my application, the Legend digit is displayed in a window panel that the user can open and close at their discretion. This application also has a ArcGISDynamicMapServiceLayer whose symbology can be specified by the user using the esri.tasks.GenerateRendererTask. Given the following scenario, the Legend does not get updated properly.

Assume that I have generated a renderer with esri.tasks.ClassBreaksDefinition and specified 5 breaks. The map is displayed as I wish it to be. I open my legend window, which instantiates the esri.dijit.Legend and calls its startup(). I can see that it sends the legend REST request to the ArcGISDynamicMapServiceLayer and the Legend dijit displays the correct symbology for the map as it is currently shown...with 5 classes.

Now, I close the legend window and, in so doing, the code calls the esri.dijit.Legend's destroy() and I set its variable to null. Then, I change the display of the map and specify, say, 10 breaks. The map displays as desired (with 10 classes now) and then I open my legend window again (which, instantiates a new Legend dijit and calls  startup() again). This time, though, the legend it shows me for that service will still show the 5 classes it generated the last time I opened it. And, no new legend REST request was sent to the ArcGISDynamicMapServiceLayer.

It's as though something is still in memory and whatever it is indicates to the dijit's code somehow that nothing has changed in the display since the last time the legend was drawn.

Can anyone suggest anything that could be causing this behaviour? Is there something further I can clear or free when I destroy the Legend dijit to ensure that the next time a Legend dijit is created and started within the same session it is not referencing some information that is now out of date?

Cheers,
jtm
0 Kudos
2 Replies
derekswingley1
Deactivated User
Are you using setDynamicLayerInfos on your dynamic service before re-creating your legend?
0 Kudos
JoanneMcGraw
Frequent Contributor
swingley,

Thank you for your reply/suggestion. I am not using setDynamicLayerInfos on my dynamic service before re-creating my legend, but would be happy to do so...if I could only understand how.

I've found a couple of different explanations of the method online and about 3 or 4 examples of its use but, so far, have been unable to apply any of what I am seeing in them to my code in a way that works. The closest thing I've come up with (as far as I can tell regarding how I think it is supposed to work) is to try to set the layer's DynamicLayerInfos in the callback function where the generated renderer is applied:
        cbExecuteGenerateRendererTask: function(renderer){
            var optionsArray = [];
            var drawingOptions = new esri.layers.LayerDrawingOptions();
            drawingOptions.renderer = renderer;
            optionsArray[this.layerId] = drawingOptions;
            this.layer.setLayerDrawingOptions(optionsArray);
            this.layer.setVisibleLayers([this.layerId]);

// -------- 
// this.layer is instantiated elsewhere as: new esri.layers.ArcGISDynamicMapServiceLayer("serviceUrl");
// That mapping service contains 7 layers of data; only one is visible at any one time and a single 
// attribute from that visible layer is used to generate the renderer for it.
//---------

            if (this.dynamicLayerInfos === null){
                this.dynamicLayerInfos = this.layer.createDynamicLayerInfosFromLayerInfos();
            }
            var dynamicLayerInfos = [];
            var dynamicLayerInfo = new esri.layers.DynamicLayerInfo();
            dynamicLayerInfo.id = this.layerId; 
            dynamicLayerInfo.source = this.dynamicLayerInfos[this.layerId];
            dynamicLayerInfos.push(dynamicLayerInfo);
            this.layer.setDynamicLayerInfos(dynamicLayerInfos);
        }


I've tried doing the dynamicLayerInfos part before and after setting the drawing options and before and after setting the visible layers. The message I receive when "this.layer.setDynamicLayerInfos(dynamicLayerInfos);" makes the export request is invariably:

{"error":{"code":400,"message":"Missing mapLayerId for dynamic mapLayer with 'id': 1.","details":[]}}


I'm not sure what "mapLayerId" it is referring to. There is a mapLayerId provided for the source in the JSON of the request, so I'm guessing it's not that one. The export request sent when I setDynamicLayerInfos on the service looks like this:
http://<mydomain>/arcgis/rest/services/<myservice>/MapServer/export?dpi=96&transparent=true&format=png8&layers=show:-1&dynamicLayers=[{
    "id": 1,
    "source": {
        "defaultVisibility": false,
        "id": 1,
        "maxScale": 0,
        "minScale": 0,
        "name": "Crops (vegetables, fruits, berries, nuts, nursery, sod, Christmas trees, greenhouse, mushrooms, and maple)",
        "parentLayerId": -1,
        "subLayerIds": null,
        "source": {
            "type": "mapLayer",
            "mapLayerId": 1
        }
    },
    "drawingInfo": {
        "renderer": {
            "type": "classBreaks",
            "field": "FRUIT_FARM_NUM",
            "minValue": 0,
            "classBreakInfos": [{
                "classMaxValue": 14,
                "label": "0 - 14",
                "description": "",
                "symbol": {
                    "color": [204, 255, 204, 255],
                    "type": "esriSFS",
                    "style": "esriSFSSolid"
                }
            }, {
                "classMaxValue": 59,
                "label": "15 - 59",
                "description": "",
                "symbol": {
                    "color": [130, 217, 142, 255],
                    "type": "esriSFS",
                    "style": "esriSFSSolid"
                }
            }, {
                "classMaxValue": 165,
                "label": "60 - 165",
                "description": "",
                "symbol": {
                    "color": [72, 179, 100, 255],
                    "type": "esriSFS",
                    "style": "esriSFSSolid"
                }
            }, {
                "classMaxValue": 298,
                "label": "166 - 298",
                "description": "",
                "symbol": {
                    "color": [28, 142, 73, 255],
                    "type": "esriSFS",
                    "style": "esriSFSSolid"
                }
            }, {
                "classMaxValue": 498,
                "label": "299 - 498",
                "description": "",
                "symbol": {
                    "color": [0, 104, 55, 255],
                    "type": "esriSFS",
                    "style": "esriSFSSolid"
                }
            }]
        }
    }
}]&bbox=-14896912.62163498,3215744.8139635143,-5700009.378365019,10123206.186036486&bboxSR=102100&imageSR=102100&size=940,706&f=json


I've also tried playing around with this a bit in the "onUpdateEnd" handler but I can't get it to work there either and don't believe it would help there any more than in the GenerateRendererTask callback anyway.

Can you be a bit more forthcoming re: the usage of this method?

Cheers,
jtm
0 Kudos