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.
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.
Solved! Go to Solution.
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.
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.
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.
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
Rene,
I think I got it. I was not setting whereclause on the query object.
Appreciate your help Rene.