Select to view content in your preferred language

One button to independently trigger functions of users' choice

1901
10
Jump to solution
05-20-2014 01:56 PM
AlexGole1
Emerging Contributor
I am looking to simplify my code. As of right now I have a code that allows the users to extract GIS data in three different ways. 1) Extract by Drawn extent, 2) Extract by extent of the a county, 3) Extract by Extent by Unit.

1st function:

   
function extractData() {             //get clip layers             var clipLayers = [];             if (registry.byId("layer1").get("checked")) {                clipLayers.push("SRA");             }             if (registry.byId("layer2").get("checked")) {                clipLayers.push("Burn");             }             if (clipLayers.length === 0|| map.graphics.graphics.length === 0) {                alert("Select layers to extract and draw an area of interest.");                return;             }              var featureSet = new FeatureSet();             var features = [];             features.push(map.graphics.graphics[0]);             featureSet.features = features;              var params3 = {                "Layers_to_Clip": clipLayers,                "Area_of_Interest": featureSet,                "Feature_Format": registry.byId("formatBox").get("value")             };              domStyle.set(loading2, "display", "inline-block");             gp.submitJob(params3, completeCallback, statusCallback2, function (error) {                alert(error);                domStyle.set(loading2, "display", "none");             });          }

2nd fucntion:

   
function executeQueryTask(county) {    var clipLayers = [];             if (registry.byId("layer1").get("checked")) {                clipLayers.push("SRA");             }             if (registry.byId("layer2").get("checked")) {                clipLayers.push("Burn");             }    if (clipLayers.length === 0) {                alert("No layers to clip, please select a layer you would like to clip...");                return;             }         var county = document.getElementById("sel_county").value                   var queryTask = new QueryTask("http://webgisdevint1/arcgis/rest/services/Alex_Try/Counties/MapServer/0");     queryTask.on("complete", addToMap)     var query = new Query();           query.returnGeometry = true;           query.outFields = ["NAME_PCASE"];           query.where = "NAME_PCASE = '" + county + "'";           query.outSpatialReference = map.spatialReference;     queryTask.execute(query, function (featureSet) {    var AOI = featureSet.features[0].geometry;    var graphic = new Graphic(AOI, symbol);       var features = [];             features.push(graphic);             var fSet = new FeatureSet();             fSet.features = features;           var params = {                "Layers_to_Clip": clipLayers,                "Area_of_Interest": fSet,                "Feature_Format": registry.byId("formatBox").get("value")   };                       domStyle.set(loading, "display", "inline-block");             gp.submitJob(params, completeCallback, statusCallback, function (error) {                alert(error);                domStyle.set(loading, "display", "none");             });     });    }


And a third that looks exactly like the  one above except that I query by units not county.

Each function is triggered by this:

 
  registry.byId("extract1").on("click", executeQueryTask);


And this:

    <button id="extract1" data-dojo-type="dijit/form/Button" data-dojo-props="iconClass:'Query', showLabel:false">              Extract Data             </button>

Now here is my question: How can independently trigger each function from ONE AND ONLY ONE button? As of right now I have three buttons that triggers each function. Would it be possible to trigger each function independently from only 1 button? For example, If users draw then the 1 and only "extract" button would extract data by the extent drawn by the user. FYI I am using a dijit/from/dropdown for selecting counties and units, which means there always be one element selected in these two extraction process.
Thank you,
Alex
0 Kudos
10 Replies
AlexGole1
Emerging Contributor
You can define any sort of function you'd like, but until you call it, it won't do anything. You'd essentially have to do something like this

  case "extractByUnit":
    executeQueryTask1(unit);
    function executeQueryTask1(unit) {
      var clipLayers = [];
      if (registry.byId("layer1").get("checked")) {
        clipLayers.push("SRA");
      }
      etc.


Here's an example showing the difference. Try clicking all three buttons to see what happens.

As for your "addTomap" error, you probably have the function name misspelled. But you say

Remember that JavaScript is case sensitive.


Thank you for your explanation. It makes more sense now!

The exact error I get is the following: TypeError: feature is undefined. I am not sure why but its blocking the script from running fine.

feature is located here:
function addToMap2(results){
    map.graphics.clear();
    var featureArray = results.featureSet.features;
          var feature = featureArray[0];
          var graph = map.graphics.add(feature.setSymbol(symbol).setInfoTemplate(infoTemplate));
    var extent = esri.graphicsExtent(map.graphics.graphics);
    map.setExtent(extent, true);
    
    }
0 Kudos