5 Replies Latest reply on Nov 9, 2007 7:08 PM by ntsiii

    handling event listeners - returns from async queries...

      i'm a longtime programmer, but relatively new to async calls. so i think i'm missing something basic...

      when my current flex screen loads, it performs three queries to pre-populate the screen. first query is the "header" record, second query gets the "line item" records, and the third query gets values for a picklist which will be in the data grid. (note that i'm querying a web service - although i doubt that matters much...)

      when each query is finished and the local objects (ArrayCollections, etc) are populated, i want to perform a specific action. So my first thought is to write three "handle*QueryComplete" methods, and add an event listener to the connection before each query. So these three lines will appear in my code:
      apex.addEventListener(QueryEvent.QUERY_EVENT, handleHeaderQueryCompleted);
      apex.addEventListener(QueryEvent.QUERY_EVENT, handleLineItemQueryCompleted);
      apex.addEventListener(QueryEvent.QUERY_EVENT, handleProdQueryCompleted);

      But then when one qeury finishes, it will call all three handle* methods, right? How am I supposed to do this so that when each query completes, it performs the proper action?

      thanks for your time
        • 1. Re: handling event listeners - returns from async queries...
          you must use a Dispatch event after each query is finished that is something like this

          DispatchEvent(QueryEvent) i don't know exactly the syntax but search for dispatch event ond docs.

          Although you might have your reasons, why don't you use the result property of webservices?

          Width flex you can make in each operation like this

          <webservice id="mywebservice" wsdl="mypath">
          <operation name="mywebserviceoperationname" result="What i want to do when data arrives(event)/>

          and then with actionscript you would go like this

          private myresultfunction(e:resultevent){

          actions to take

          • 2. Re: handling event listeners - returns from async queries...
            levancho Level 3
            batmitra sorry but I dont find dispatchEvent sutable here at all, you answer is very misleading,

            cmarkiewicz as you say you have three queries that means I assume you have three services for it, it does not matter its http or webservice , this also means that you can assign seperate resulthandler to each service following way :

            myheaderQueryService.addeventListener( ResultEvent.RESULT,handleHeaderQueryCompleted);

            myLineItemQueryService.addeventListener( ResultEvent.RESULT,handleLineItemQueryCompleted);

            myProdQueryService.addeventListener( ResultEvent.RESULT,handleProdQueryCompleted);

            this way each function will be called when corresponding service is back,

            all this is good if your functions are not inter-dependand meaning that for example : headerQuery must be back before lineitems query, if you want that then you have to manage syncronization yuorselfe,because its not guaranteed that the order you call services will be the same order they return to you.

            • 3. Re: handling event listeners - returns from async queries...
              levancho Level 3
              one way to achive syncroniation is to chain the calls and handlers of those three queries following way :

              myheaderQueryService.addeventListener( ResultEvent.RESULT,handleHeaderQueryCompleted);

              private function handleHeaderQueryCompleted(e:resultEvent): void {
              // do some codeing and then call another service
              myLineItemQueryService.addeventListener( ResultEvent.RESULT,handleLineItemQueryCompleted);

              etc ...
              • 4. Re: handling event listeners - returns from async queries...
                cmarkiewicz Level 1
                levancho - this makes sense - thanks for the reply. the order in which the queries are executed is not relevant, so i will write it as you recommend.
                • 5. Re: handling event listeners - returns from async queries...
                  ntsiii Level 3
                  In order to identify a result with its call, to perform some specific processing, you can use the AsyncToken. Below is some sample code.

                  Sample code using HTTPService, e4x, handler function to populate a list item.
                  Also shows usage of AsyncToken.

                  The DataGrid tag:
                  <mx:DataGrid id="dg" dataProvider="{_xlcMyListData}" .../>

                  The HTTPService tag:
                  <mx:HTTPService id="service" resultFormat="e4x" result="onResult(event)" fault="..../>

                  Script block declaration:
                  import mx.rpc.Events.ResultEvent;
                  [Bindable]private var _xlcMyListData:XMLListCollection;

                  Invoke send:
                  var oRequest:Object = new Object();
                  oRequest.Arg1 = "value1";
                  var callToken:AsyncToken = service.send(oRequest);
                  token.callId = "myQuery1";

                  Result Handler function:
                  private function onResult(oEvent:ResultEvent):void {
                  var xmlResult:XML = XML(event.result); //converts result Object to XML. can also use "as" operator
                  var xlMyListData:XMLList = xmlResult.myListData; //depends on xml format, is row data
                  _xlcMyListData = new XMLListCollection(xlMyListData); //wrap the XMLList in a collection
                  trace(_xlcMyListData.toXMLString()); //so you can see exactly how to specify dataField or build labelFunction
                  var callToken:AsyncToken = oEvent.token;
                  var sCallId = callToken.callId; //"myQuery1"
                  switch(sCallId) { //Process the result conditionally
                  case "myQuery1":
                  doQuery2(); //do whatever. this example calls another data service query