How to handle QueryTask call back function in AMD module?

5646
4
Jump to solution
01-30-2015 12:18 PM
SanajyJadhav
Occasional Contributor II

Hello,

 

I have just started learning ArcGIS API For JavaScript and Dojo AMD and I'm stuck on an issue of callback function of a query task.

 

I have a simple html page and couple of custom AMD modules inside a folder.Below is my folder structure.

 

Folder Structure.png

Below is my AMD module for querying feature layer added to map and I call function "queryStatesLayer" from my index.html page.

 

define(["esri/tasks/QueryTask","esri/tasks/query","dojo/_base/lang"],function(QueryTask,Query,lang){     return{    queryStatesLayer:function (sqlString,Layer,outFlds){    var query,queryTask;    //initialize query task   queryTask = new QueryTask(Layer.url);    //initialize query   query = new Query();   query.returnGeometry = true;   query.outFields = outFlds;    //execute query   queryTask.execute(query,showResults);   },    //query completed Callback function.   showResults: function(qryResults){   alert("Query Completed..");  } ...    }; }); //define function ends..

 

Now,my problem is I'm not being able to figure out how should I write my query completed call back function in my module.I know that the way it is written now will not be called when query is completed. And if I write it as simple function outside of return { };it not recognized and I get an error in the console of Mozilla Firefox.

 

I would appreciate if I could get guidance on this issue.I want to write my query completed callback function in my custom AMD module.

 

S.

0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Frequent Contributor

The way you are doing it, you can just use an anonymous function in the execute method and it will work.

queryTask.execute(query, function(queryResults) { ... });

Or you could return the execute method.

return queryTask.execute(query);

Then whatever function called it could handle the callback.

myTaskObject.queryStatesLayer('NAME').then(function(queryResults) { ... });

The execute method of a QueryTask is a Promise, handled by dojo/Deferred.

I hope that helps.

View solution in original post

0 Kudos
4 Replies
ReneRubalcava
Frequent Contributor

The way you are doing it, you can just use an anonymous function in the execute method and it will work.

queryTask.execute(query, function(queryResults) { ... });

Or you could return the execute method.

return queryTask.execute(query);

Then whatever function called it could handle the callback.

myTaskObject.queryStatesLayer('NAME').then(function(queryResults) { ... });

The execute method of a QueryTask is a Promise, handled by dojo/Deferred.

I hope that helps.

0 Kudos
SanajyJadhav
Occasional Contributor II

Rene,

Thank you for your reply.

I tried the first approach of anonymous function but it did not work. Code in that anonymous function is not getting called. Obviously I missed something. Below is my code.

queryTask.execute(query, function(queryResults) {

  alert('done');

  });

Can you please give me a sample code for this? it would be really helpful for me.I would admit that being a Silverlight API programmer, JavaScript API is bit difficult for me as this moment.

Cheers,

S.

0 Kudos
ReneRubalcava
Frequent Contributor

Your query seems to be missing a where property.

You can also set the query.text, but I always felt it was safer to be explicit and use the where property.

If you wanted all results, send a where of "1=1";

query.where = "1=1";

Here is a sample from the Esri site using QueryTask and Query

Query data without a map | ArcGIS API for JavaScript

SanajyJadhav
Occasional Contributor II

Rene,

I think I got it. I was not setting whereclause on the query object.

Appreciate your help Rene.

0 Kudos