Select to view content in your preferred language

calling webservices from action script - order of events?

1233
2
02-16-2011 09:53 AM
grahamcooke
Regular Contributor
I have a problem that I cannot seem to easily solve with a call out to a webservice. i have created an actionscript class to act as a webservice handler. In this class i set up my webservice and the corresponding events and declare a property getter to return the arraycollection that holds the resulting table from the .net webservice.

this is my class:
public function getWebServiceData(_selectedMortarSize:int, _numStepMaxCharge:int, _piIllum:String, _piUSAmmo:String):void
 {
                  selectedMortarSize = _selectedMortarSize;
                  numStepMaxCharge = _numStepMaxCharge;
                  piIllum = _piIllum;
                  piUSAmmo = _piUSAmmo;
                  ws = new WebService();
                  ws.wsdl = "http://lmgis02/cawdapt_ws/cawdapt_dotnet_ws.asmx?wsdl"
                  ws.getMortarChargeData.resultFormat = "object";     
                  ws.getMortarChargeData.addEventListener("result", myResultHandler);                       ws.getMortarChargeData.addEventListener("fault", myFaultHandler);  

                  //need to set up a load result handler to catch when the webservice is loaded. From here we will
                  //pass the parameters and then the results will be returned in the result handler code.
                  ws.addEventListener(LoadEvent.LOAD, loadHandler);
                  //call webservice, result event handler will populate the arraycollection which will then be passed to the action script. 
                 ws.loadWSDL();
}

protected function loadHandler(event:LoadEvent):void 
{     

                  // send() takes the service parameters   
                 ws.getMortarChargeData.send(selectedMortarSize, numStepMaxCharge, piIllum, piUSAmmo);
} 

            protected function myResultHandler(event:ResultEvent):void
            {    

                 myMortarChargeData = new ArrayCollection();
                  myMortarChargeData = event.result.Tables.mortarChargeData.Rows;
            }  

            protected function myFaultHandler(event:FaultEvent):void 
            {     
                  Alert.show(event.toString()); 
            }


i then have a procedure in my widget code that instantiates this class, and (hopefully!) gets the arraycollection and then passes that array collection into another routine that plots a polygon


//Code that calls the webservice handler:
var myWS:MortarsWS = new MortarsWS
myWS.getWebServiceData(selectedMortarSize, numStepMaxCharge.value, piIllum, piUSAmmo);
mortarChargeData = myWS.MortarChargeData;

//code that uses the mortarChargeData

//instantiate FP class with standard trace variables setup
x = new myClass(mortarChargeData, [othervariables])


This is all initiated on a user button click, the parameters i am passing to the webservice are set up by the users interaction with the controls on the widget. The problem I am having is that the loadresult call in the webservice handler is not firing until AFTER the code has dropped into myClass and attempted to use items from the mortarChargeData arraycollection.

Why are the webservice loadhandler, the resultant send operation and the result event handler all firing after I intended them to? Can anyone see what I am doing wrong?

Hope someone can help!
Tags (2)
0 Kudos
2 Replies
ReneRubalcava
Esri Frequent Contributor
myWS.getWebServiceData(selectedMortarSize, numStepMaxCharge.value, piIllum, piUSAmmo);
// this happens before data is returned
mortarChargeData = myWS.MortarChargeData;


The problem you are having is you are trying to get the results of your service call from your myWS object before myWS has had a chance to get the process the results.

You could have your myWS object dispatch an Event.COMPLETE when it's done, so it would look like
// have your myWS extend EventDispatcher
protected function myResultHandler(event:ResultEvent):void
{    
    myMortarChargeData = new ArrayCollection();
    myMortarChargeData = event.result.Tables.mortarChargeData.Rows;
    dispatchEvent(new Event(Event.COMPLETE));
}


Then listen for it
// just an example, you should probably use a named function
myWS.addEventListener(Event.Complete, function(e:Event):void { mortarChargeData = myWS.MortarChargeData; });
myWS.getWebServiceData(selectedMortarSize, numStepMaxCharge.value, piIllum, piUSAmmo);
0 Kudos
grahamcooke
Regular Contributor
perfect! thankyou 🙂
0 Kudos