query - return success or fail

1942
3
Jump to solution
04-22-2016 10:41 AM
DaveOrlando
Occasional Contributor III

curious how to return a Boolean within the lang.hitch environment.

I would like to :

call query and return true or false so I know to continue or not

I am currently using lang.hitch for the query's return function and just chaining my next query within that, I would like to call the second query from outside of this.

something like:

var pass = Query1();

if (pass) Query2

here is some code

// Step 1
getEvacGeom: function (area, status) { //, order, alert, recind) {
 var pass = false;
 var queryTask = new QueryTask(this.EvacLayer);
 var query = new Query();
 query.returnGeometry = true;
 query.outFields = ["*"];
 query.outSpatialReference = this.map.spatialReference;
 query.where = "AreaName = '" + area + "' and Status = '" + status + "'";
 queryTask.execute(query, lang.hitch(this, EvacGeom_returned), lang.hitch(this, EvacGeom_error));

 function EvacGeom_returned(results) {
  if (results.features.length > 0) {
   for (var i = 0; i < results.features.length; i++) {
    var feature = results.features;
    if (feature.attributes["EvacType"] == "Order")
     feature.setSymbol(symbolOrder);
    else if (feature.attributes["EvacType"] == "Alert")
     feature.setSymbol(symbolAlert);
    else if (feature.attributes["EvacType"] == "Recind")
     feature.setSymbol(symbolRecind);
    _glEvacs.add(feature);
   };
   // call Step 2 OLD WAY
   //this.getParcelsByEvacExtent(evacExtent);
   
   // NEW ATTEMPT
   pass = true;
   // ? return pass;
  }
 }
 function EvacGeom_error(e) {
  busyList.hide();
  alert(e.message)
 };
 return pass; // this just returns the original false......
},
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Esteemed Contributor

Dave,

  That is a situation for a Deferred.

require 'dojo/Deferred',
Deferred

// Step 1
getEvacGeom: function (area, status) { //, order, alert, recind) {
 var def = new Deferred()
 var pass = false;
 var queryTask = new QueryTask(this.EvacLayer);
 var query = new Query();
 query.returnGeometry = true;
 query.outFields = ["*"];
 query.outSpatialReference = this.map.spatialReference;
 query.where = "AreaName = '" + area + "' and Status = '" + status + "'";
 queryTask.execute(query, lang.hitch(this, EvacGeom_returned), lang.hitch(this, EvacGeom_error));
  
 function EvacGeom_returned(results) {
  if (results.features.length > 0) {
   for (var i = 0; i < results.features.length; i++) {
    var feature = results.features;
    if (feature.attributes["EvacType"] == "Order")
     feature.setSymbol(symbolOrder);
    else if (feature.attributes["EvacType"] == "Alert")
     feature.setSymbol(symbolAlert);
    else if (feature.attributes["EvacType"] == "Recind")
     feature.setSymbol(symbolRecind);
    _glEvacs.add(feature);
   };
   def.resolve(true);
  }
 }
 function EvacGeom_error(e) {
  busyList.hide();
  alert(e.message);
  def.resolve(false);
 };
 return def;
},

View solution in original post

0 Kudos
3 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Dave,

  That is a situation for a Deferred.

require 'dojo/Deferred',
Deferred

// Step 1
getEvacGeom: function (area, status) { //, order, alert, recind) {
 var def = new Deferred()
 var pass = false;
 var queryTask = new QueryTask(this.EvacLayer);
 var query = new Query();
 query.returnGeometry = true;
 query.outFields = ["*"];
 query.outSpatialReference = this.map.spatialReference;
 query.where = "AreaName = '" + area + "' and Status = '" + status + "'";
 queryTask.execute(query, lang.hitch(this, EvacGeom_returned), lang.hitch(this, EvacGeom_error));
  
 function EvacGeom_returned(results) {
  if (results.features.length > 0) {
   for (var i = 0; i < results.features.length; i++) {
    var feature = results.features;
    if (feature.attributes["EvacType"] == "Order")
     feature.setSymbol(symbolOrder);
    else if (feature.attributes["EvacType"] == "Alert")
     feature.setSymbol(symbolAlert);
    else if (feature.attributes["EvacType"] == "Recind")
     feature.setSymbol(symbolRecind);
    _glEvacs.add(feature);
   };
   def.resolve(true);
  }
 }
 function EvacGeom_error(e) {
  busyList.hide();
  alert(e.message);
  def.resolve(false);
 };
 return def;
},
0 Kudos
DaveOrlando
Occasional Contributor III

thanks,

looks simple in concept, but it returns def before return or error are ever reached.

is this not the concept?

launchQuery: function () {
  var stepOneSuccess = this.getEvacGeom(selAreaName, selStatus);
  if (stepOneSuccess) {...}
},
0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Dave,

  No it would look like:

this.getEvacGeom(selAreaName, selStatus).then(function(result){
  if(result) {
    //Do something
  }
});