having trouble returning an array to a function that's using esri.request

453
1
06-20-2013 11:22 AM
JoanSteinbacher
Occasional Contributor
I have downloaded the Basic Viewer template. I am creating my webmap with code. I'm trying to create a function that will return an array of fields I can use in the 'fieldInfos' setting of the 'popupInfo', so I don't have to hardcode them. Here's the code for my operational layer:

           //Existing Land Use (popup)
           { "url": "http://gis.tpcmaps.org/ArcGIS/rest/services/LandUse/Existing_Land_Use/MapServer/0",
             "visibility": true,
             "opacity": 0,
             "mode": 2,  //Selection-only
             //"visibleLayers": [],
             "layerDefinition": {
              "minScale": 100000,
              "maxScale": 0,
             },
             "title": "popup Existing Land Use",
             "id": "popelu",
             "popupInfo": {
          "title": "Existing Land Use",
          "fieldInfos": createFieldInfos(), 


Using the following function works as expected, so I know the approach is do-able.

function createFieldInfos() {
    console.log("-->inside createFieldInfos function");
    var thearray = [{'fieldName': 'JURISDICTION','label': 'JURISDICTION','visible':true},{'fieldName': 'ELUSHADE','label': 'EXISTING LU','visible':true},{'fieldName': 'ELU2_DESC','label': 'ELU DESCRIPTION','visible':true},];
    return thearray;
}


But when I try returning the array in this function, it's not working.

function createFieldInfos() {
 console.log("-->inside createFieldInfos function");
 var endpoint = "http://gis.tpcmaps.org/ArcGIS/rest/services/LandUse/Existing_Land_Use/MapServer/0";
 //esri.request returns a deferred.
 var jsonobject = esri.request({
  url: endpoint,
  content: { f: 'json' },
  callbackParamName: 'callback',
  //load: processServiceInfo, //The load property specifies a function that will execute once the content has been successfully downloaded.
  //error: errorHandler  
 });
 
 //If the json object is successfully returned, get the fields from the layer.
 jsonobject.then(
   function (info) {
     console.log('layer info: ', info);
  console.log("info.name: " + info.name + ",info.id: " + info.id);
  
         var fieldInfos = "[";
     
  dojo.forEach(info.fields, function(field) {
   console.log( "Name: " + field.name + ", Alias: " + field.alias);
   if (field.name != "Shape" & field.name != "OBJECTID") {
    fieldInfos += "{'fieldName': '" + field.name + "',";
    fieldInfos += "'label': '" + field.alias + "',"; 
    fieldInfos += "'visible':" + true + "},";    
   }
  });
  
  fieldInfos += "]";
  console.log("fieldInfos: " + fieldInfos);
  //convert the string into an array.
  fieldInfosArray = eval(fieldInfos);
  console.log("fieldInfosArray.length: " + fieldInfosArray.length);
  return fieldInfosArray;
   },
   function (error) {
     console.log("Error: ", error.message);
   }
 );
return jsonobject;
}


I suspect it's some scoping issue?
0 Kudos
1 Reply
JacksonGilman1
Occasional Contributor
    // the actual return value from esri.request is a promise 
    // (...sort of, technically it's a legacy dojo Deferred object decorated with a 'then' method)
    jsonobject.then(function (info) {
            var arr = dojo.map(info.fields, function (field) {
                return {
                    fieldName: field.name,
                    label: field.alias,
                    visible: true
                }
            });
            console.log(arr);
            // returning a value from the callback may not be enough...
            //return arr;
            passTheArrayToWhateverYouNeedToUpdate(arr);
        }
    );


Hope that helps some!
0 Kudos