Get the Max number from a field

1239
7
Jump to solution
08-30-2018 01:59 PM
TheKenerson
Occasional Contributor

Just to make using deferred a bit clearer, may i ask you for an example of using deferred within 2-3 methods?

I'm working on a problem where i am trying to get the max number from a field.

So first a query method is called (which sets uo the query), and then at query.execute, the query is sent to another method which cycles through each record, places the number from the field in an array, and then the highest number is selected.

I then need to get to pass out this max number back up the chain. I was wondering if you had an example?

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

dojo/Deferred — The Dojo Toolkit - Reference Guide 

Esri deferred's are much more simple then the above link though

  var query = new Query();
  var queryTask = new QueryTask( ... );
  query.where = "STATE_NAME = 'Washington'";
  query.outSpatialReference = {wkid:102100}; 
  query.returnGeometry = true;
  query.outFields = ["CITY_NAME"];
  queryTask.execute(query, function(responce){
   console.info(response);
  });

So queryTask.execute is a deferred meaning that the code in the function callback will not execute until the deferred is resolved. So if you want to use some bit of info from the results of the query then you need to put that code in the callback function (where the console.info(response); is. If you need other functions to work with some bit of info from the query then those functions need to be called from inside the callback function too.

Hope this helps.

View solution in original post

7 Replies
RobertScheitlin__GISP
MVP Emeritus

The Kenerson,

   First off you do not try and get the max value by looping though the whole feature layer on the client side, you let the server do that for you.

Here is a sample for field statistics:

Query Statistics with SQL Expression | ArcGIS API for JavaScript 3.25 

Let me know if you still have an issue with deferreds.

TheKenerson
Occasional Contributor

Thank you for your reply and for many of the past replies. 

I definitely still have an issue with deferreds. They seem like they are along the same idea as "awaits" in C#, but i'm much less informed on js than C#, so I find the syntax confusing. Can you point me in the right direction?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

dojo/Deferred — The Dojo Toolkit - Reference Guide 

Esri deferred's are much more simple then the above link though

  var query = new Query();
  var queryTask = new QueryTask( ... );
  query.where = "STATE_NAME = 'Washington'";
  query.outSpatialReference = {wkid:102100}; 
  query.returnGeometry = true;
  query.outFields = ["CITY_NAME"];
  queryTask.execute(query, function(responce){
   console.info(response);
  });

So queryTask.execute is a deferred meaning that the code in the function callback will not execute until the deferred is resolved. So if you want to use some bit of info from the results of the query then you need to put that code in the callback function (where the console.info(response); is. If you need other functions to work with some bit of info from the query then those functions need to be called from inside the callback function too.

Hope this helps.

TheKenerson
Occasional Contributor

Thank you. I understand your example. That makes sense.

In my code, I am calling the query and query task method from the attribute-change event within the smart editor widget. So, I would like to return this max number value back to that function. 

To say it differently, in your example, I can see how I could pass the (response) value to a new method, but i'm not sure how I would return/pass the response value to the method that originally called the query.

Can you show me an example of how this could be done?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

So in your attribute-change event handler you call a function that returns a deferred.

function getMax(evt){
  var def = new Deferred();
  var query = new Query();
  var queryTask = new QueryTask( ... );
  query.where = "STATE_NAME = 'Washington'";
  query.outSpatialReference = {wkid:102100}; 
  query.returnGeometry = true;
  query.outFields = ["CITY_NAME"];
  queryTask.execute(query, lang.hitch(this, function(responce){
   def.resolve(response);
  }));
  return def;
}‍‍‍‍‍‍

When you call this function you do this:

getMax().then(function(result){
  //Now you have the result of the getMax function
});

So this is a deferred calling a deferred.

Don't forget to mark this question as answered by clicking on the "Mark Correct" link on the reply that answered your question.

TheKenerson
Occasional Contributor

Thank you. This is great. Works like a charm. JavaScript is funky. Robert, I know this is a separate question, but is there a particular reference book that you recommend that offers a good background in this type of javascript? This forum is great, but I was wondering if there was another reference I could review that might be helpful.

Thanks,
Scott

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Scott,

  There are not any good books I would recommend personally. I have always just used the dojo doc site and the esri samples.

0 Kudos