Variable scope within function for querytask

1045
5
03-20-2012 06:08 AM
AdrianMarsden
Occasional Contributor III
Hi

I'm trying to hack about with the query tasks to get a YEs/No reply to a simple question - does the point passed to a page fall within our area

I have code like (x & y get defined earlier from URL parameters)

 function checkus() {
 
  queryTask = new esri.tasks.QueryTask("http://<our server>/ArcGIS2/rest/services/eforms2/MapServer/0");
  var point = new esri.geometry.Point(x, y, new esri.SpatialReference({wkid:27700}));
  query = new esri.tasks.Query();
  query.returnGeometry = false;
  query.geometry = point 
  query.outFields = ["admin_name"];
  queryTask.execute(query);
  dojo.connect(queryTask, "onComplete", function (results) {
   if (results.features.length>0) {
    alert("In Our Area");
   }
   else {    
    window.location = "NoUs.asp"
   } 
      }  
    
  );



}


I am trying to insert a variable inside the the bit that looks for the number of records returned, but try as I can, I can't get the variable to be global, as soon as that part finishes, so, it appears does the variable.

I must be doing something totally daft here.

Cheers

ACM
0 Kudos
5 Replies
MichaelJenkins
Occasional Contributor III
You should be able to do that as long as you declare the variable outside of any functions.

             var resultCount; 
             function checkus() {
 
  queryTask = new esri.tasks.QueryTask("http://<our server>/ArcGIS2/rest/services/eforms2/MapServer/0");
  var point = new esri.geometry.Point(x, y, new esri.SpatialReference({wkid:27700}));
  query = new esri.tasks.Query();
  query.returnGeometry = false;
  query.geometry = point 
  query.outFields = ["admin_name"];
  queryTask.execute(query);
  dojo.connect(queryTask, "onComplete", function (results) {
   if (results.features.length>0) {
    resultCount=results.features.length;
    alert("In Our Area");
   }
   else {    
    window.location = "NoUs.asp"
   } 
      }  
    
  );
}
GISP
0 Kudos
AdrianMarsden
Occasional Contributor III
Cheers - I'm pretty sure I tried all combinations of where and how to declare the variable.  I'll try it all later when I'm at work, although I think overnigt, I've decided to re-do it using an identify task as there will be other layers I want to check against, which may vary.

Ta

ACM
0 Kudos
nicogis
MVP Frequent Contributor
However if you want call a method in a given scope use dojo.hitch
http://dojotoolkit.org/reference-guide/1.7/dojo/hitch.html
(see sample < 1.7)
also here there is an example: http://www.ibm.com/developerworks/web/library/wa-aj-dojo/
0 Kudos
AdrianMarsden
Occasional Contributor III
Thanks for that, although that it stretching my (limited) skills I'm afraid.

Declaring it global outside the function certainly doesn't work I'm afraid.

ACM
0 Kudos
JeffPace
MVP Alum
If x and y are defined earlier, you either need to

checkus (x.y) and pass them to the function, or call

dojo.hitch (this, "checkus") to keep within scope.
0 Kudos