Send extra data to Callback function

745
5
10-14-2013 03:05 AM
Nadeera_RukshanMemmenda_Arachc
New Contributor
I�??m calling selectFeatures(query,selectionMethod?,callback?,errback?) method in a loop and I need to know the feature layer of each result. I call the same callback function for each call inside the loop.
I tried several ways but neither worked.

Followed this link as well: http://directwebremoting.org/dwr/documentation/browser/extra-data.html

What I want to do is to list the number of features returned from each feature server.

Please help�?�
0 Kudos
5 Replies
JasonZou
Occasional Contributor III
This is what you can do.


  1. Define a object that contains the selected feature count for each feature layer. Remember to reset the object to empty object prior to each selection.

  2. var selectedFeatureCount = {};

  3. Change your callback function to accept two parameters: the feature layer and the result returned from selectFeatures.

  4. function selectCallback(featureLayer, selectResult) {
       selectedFeatureCount[featureLayer.id] = selectResult.features.length;
       // process your select result
    }

  5. Change selectFeatures statement as below.

  6. featLayer.selectFeatures(query, selectionMethod, function(result) {
        selectCallback(featLayer, result);
    },errback?)

0 Kudos
Nadeera_RukshanMemmenda_Arachc
New Contributor
I tired this and this doesn't work. :mad:

in the callback method when you try to get the feature server id, every time you get the id of the feature server you called last inside the loop. Strange, but not working.
0 Kudos
JasonZou
Occasional Contributor III
Must be something wrong inside the loop. Please post the code containing the whole loop.
0 Kudos
Nadeera_RukshanMemmenda_Arachc
New Contributor
Callback funtion:

function selectFeatureCallBackFunc(featureServerCalls, features) {

        
        AddCallbackLayerDetails(featureServerCalls, features);
       
    }



For loop:

for (k = 0; k < mapServicesList.length; k++) {
                featureLayertoSelect = mapServicesList;

                featureLayertoSelect.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW, function (result) { selectFeatureCallBackFunc(featureLayertoSelect, result) }, errback);

            }


If you debug the code and check the featureServerCalls value inside the callback function, it'll be same in each call back.
0 Kudos
JasonZou
Occasional Contributor III
I see what's going on. Since selectFeatures is an asynchronous function, the loop won't stop to wait until selectFeatures is complete. It will end up with when the first selectFeatures callback is invoked, the loop already completes, and featureLayertoSelect value will be the last feature layer for all the callbacks. This is a typical trap for async programming.

Change:
for (k = 0; k < mapServicesList.length; k++) {
                featureLayertoSelect = mapServicesList;

                featureLayertoSelect.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW, function (result) { selectFeatureCallBackFunc(featureLayertoSelect, result) }, errback);

            }


To:
dojo.array(mapServicesList, function(aFeatureLayer) {
      aFeatureLayer.selectFeatures(query, 
            esri.layers.FeatureLayer.SELECTION_NEW, 
            function (result) { selectFeatureCallBackFunc(aFeatureLayer, result); }, 
            errback
       );
}


In the above code, aFeatureLayer is a function parameter which is local to each callback function for dojo.array, which in turn should differentiate from each other.
0 Kudos