AnsweredAssumed Answered

Want to create a module for this QueryTask functionality

Question asked by schlot on Aug 10, 2015
Latest reply on Aug 11, 2015 by RWinter85

I have two external tables that contain an ID and a description.  They are the basis for a set of menus, and since they are very similar in their schema, I'm attempting to create one tool  for creating an array of code and description pairs.  In addition, I'm using this as a learning tool for defining modules and it's not going so well. These these will eventually be input for menus, as well as look

 

These are two different tables and the field names are not  identical, so I need to account for that as well.

 

In my Map module, myMap.js, I have defined this variable, which is a table I have loaded as a featureLayer, along with several other layers.

 

        var specUrl = specialtyTable.url;

 

added a listener for the map load event:

myPopulateCodeList.populateCode('spec',specUrl);

 

The function populateCode is getting called, but I need to keep the codeList array as a variable, because I'll use it again in more than one place.

 

myPopulateCodeList.js

 define([
  "dojo/on", 
  "esri/tasks/QueryTask", 
  "esri/tasks/query"
  ], function(
  on,QueryTask, Query){
  var codeList = [];
      return {
        populateCode: function(codeType, url){
          switch (codeType) {
            case "spec":
              codeAtt = 'ID_SPECIALTY_PK';
              specAtt = 'TX_SPECIALTY_DESC';
              break;
            default:
              codeAtt = 'ID_PROV_TYPE_PK';
              specAtt = 'TX_PROV_DESC';
              break;
          }
          var queryTask = new QueryTask(url);
          var query = new Query();
          query.outFields = ["*"];
          query.where = "1=1";
          query.returnGeometry = false;
          on(queryTask, 'complete', this.codeResultsHandler);
          on(queryTask, 'error', this.errorHandler);
          queryTask.execute(query);
        },
        
        //builds codeList object containing codes and descriptions 
        codeResultsHandler: function(results){
          var numResults = results.featureSet.features.length;         
            for (var j = 0; j < numResults; j++) {
              var code = results.featureSet.features[j].attributes[codeAtt];
              var desc = results.featureSet.features[j].attributes[specAtt];
              codeList.push({
                code: code,
                desc: desc
              });
            }
            console.log('end of codeList array build');
            return codeList;
          },
        
        errorHandler: function(err){
          console.log("error in myPopulateCodeList, error: " + err.details);
        }
        
      }
    return codeList;
     
    });

I can see that codeList is getting populated with values from the results handler of the queryTask, but I can't figure out how to pass this result back to the calling function.  This doesn't work:

 

var specCodeList = myPopulateCodeList.populateCode('spec',specUrl);

 

When I had it all in one big file, this was working, but I'm trying to learn how to break my work up into smaller bits.

Outcomes