You are here: > ESRI Forums > arcgis server forums > Thread Replies

ArcGIS Server Forums

ArcGIS API for JavaScript forum

Asynchronous Multiple Geoprocessing task an...   mei 09 Feb 02, 2009
Re: Asynchronous Multiple Geoprocessing tas...   Brett Lord-Castillo Feb 02, 2009
Re: Asynchronous Multiple Geoprocessing tas...   mei 09 Feb 02, 2009
Re: Asynchronous Multiple Geoprocessing tas...   Brett Lord-Castillo Feb 03, 2009
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Asynchronous Multiple Geoprocessing task and getting the results 
Author mei 09 
Date Feb 02, 2009 
Message i'm not familiar with using asynchronous Geoprocessing tasks, so if anyone can provide any advice it'll be much appreciated.

I'm trying to run 2-3 Geoprocessing Routing tasks, all calling the same GP service.
Following the sample for retrieving asynchronous GP task results on http://resources.esri.com/help/9.3/arcgisserver/apis/javascript/arcgis/help/jshelp_start.htm,

how do i detect the different job IDs in the callback that is specified so that i can use different colors for different jobs?

for eg, the sample on that webpage shows a call to:
gp.getResultData(jobInfo.jobId, "Viewshed_Result", displayResult);

with the callback function "displayResult". How do i detect which jobid in this callback function?
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Asynchronous Multiple Geoprocessing task and getting the results 
Author Brett Lord-Castillo 
Date Feb 02, 2009 
Message The link you want to the sample is:
http://resources.esri.com/help/9.3/arcgisserver/apis/javascript/arcgis/help/jshelp_start.htm#jshelp/intro_gp_running.htm

You cannot get to the jobid inside of the callback function. You can send different callback functions for different jobIds though.
So, what you have to do is build a function dynamically, either using javascript Function (note the capital F) or javascript eval.
Function lets you build a function line by line, you must use the "new" keyword.
I put an example below of using function to define the callback, following the example script from above.

I did not test this code. I don't have an asynchronomous GP service handy to test it. There may be minor syntax bugs I missed.

Basically, each string passed to new Function() is an argument to the function, and the last string is the -entire- function body. Notice in the middle, where I reach the part where I define the outline color, I have instead inserted an array element from the array "colors". It takes the modolus of the current number of jobs completed (colorindex) divided by the length of the array. So, basically, the color is rotated depending on the number of jobs completed so far and the number of colors defined in the array at the top. Notice in the array that I have to use strings there, not the color objects themselves, since really I am defining the code snippet that will get inserted into the dynamic function. jobInfo.jobId is a string - if it were an integer you could even do the modulus of jobInfo.jobId. I am not sure of the format of the jobInfo.jobId string so I don't rely on it here.
Instead of colors[colorindex%colors.length], you could also insert a function call there that returns a string representation of a dojo.color object (that would require checking how jobInfo.jobId is formatted).
 
 
//Defined at the top
//Array of colors to use - notice these are strings
var colors = ["new dojo.color([255,0,0,0.25])","new dojo.color([0,255,0,0.25])","new dojo.color([0,0,255,,0.25])"];
colorindex = 0;

//Replaces completeCallback
function completeCallback(jobInfo){
	console.log("getting data");
	var cb = new Function("results","messages",
		"var polySymbol = new esri.symbol.SimpleFillSymbol();"+
                "polySymbol.setOutline(new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 0, 0, 0.5]), 1));"+
		"polySymbol.setColor("+colors[colorindex%colors.length]+");"+
		"var features = results[0].value.features;"+
		"for (var f = 0, fl = features.length; f < fl; f++) {"+
			"var feature = features[f];"+
			"feature.setSymbol(polySymbol);"+
			"map.graphics.add(feature);}");
	gp.getResultData(jobInfo.jobId, "Viewshed_Result", cb);
	colorindex++;
}


//Delete the function "drawViewshed". It is replaced by using the dynamic function above.
 
  Brett Lord-Castillo
------------------------------
St Louis County Police
Office of Emergency Management
------------------------------
Do you dojo?
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Asynchronous Multiple Geoprocessing task and getting the results 
Author mei 09 
Date Feb 02, 2009 
Message thanks brett, think i've got it. dojo's case sensitive though, i think 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Asynchronous Multiple Geoprocessing task and getting the results 
Author Brett Lord-Castillo 
Date Feb 03, 2009 
Message Javascript is case sensitive :)
that's why you use Function() and not function().
Function() is a special constructor that returns an object of type function().
 
  Brett Lord-Castillo
------------------------------
St Louis County Police
Office of Emergency Management
------------------------------
Do you dojo?