asp.netapache-flexweborb

Connecting flex and weborb?


im a newbie in flex. Im have a question :)

I have

[Bindable] 
          private var model:AlgorithmModel = new AlgorithmModel(); 
          private var serviceProxy:Algorithm = new Algorithm( model ); 

In MXML

                    private function Show():void
        {

            // now model.Solve_SendResult = null
            while(i<model.Solve_SendResult.length) //
            {
                Draw(); //draw cube
            }
        }
                    private function Solve_Click():void
        {
            //request is a array
            Request[0] = 2;
            Request[1] = 2;
            Request[2] = 3;
            serviceProxy.Solve_Send(request);

            Show();

        }
<s:Button x="386" y="477" label="Solve" click="Solve_Click();"/>

And when i call serviceProxy.Solve_Send(request); with request is array and i want use model.Solve_SendResult in my code flex to draw many cubes use papervison3d but in the first time i received model.Solve_SendResult = null . But when I click again then everything OK.

Anyone help me? Thanks?


Solution

  • The model.Solve_SendResult object contains a result of the executed serviceProxy.Solve_Send(request) method. The Solve_Send will be executed asynchronously and as a result, at the moment when you fire the show method the Solve_SendResult object may be still null.

    As a solution, you can use the following:

    1. Create a custom event

      package foo
      {
      import flash.events.Event;
      
      public class DrawEvent extends Event
      {
      public static const DATA_CHANGED:String = "dataChanged";
      
      public function DrawEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
      {
          super(type, bubbles, cancelable);
      }
      }
      }
      
    2. In your Algorithm class define the following:

      [Event(name=DrawEvent.DATA_CHANGED, type="foo.DrawEvent")] 
      public class Algorithm extends EventDispatcher{ 
      //your code
      
    3. In the Solve_SendHandler method of the Algorithm class add the following

      public virtual function Solve_SendHandler(event:ResultEvent):void
      {
      dispatchEvent(new DrawEvent(DrawEvent.DATA_CHANGED));
      //your code
      }
      
    4. In your MXML class create onLoad method and add an event listener to an instance of the Algorithm class as it shown below:

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="onLoad()">
      
      public function onLoad():void
      {
         serviceProxy.addEventListener(DrawEvent.DATA_CHANGED, onDataChanged);
      }
      
      private function onDataChanged(event:DrawEvent):void{
      while(i<model.Solve_SendResult.length) //
          {
              Draw(); //draw cube
          }
       }
      
    5. make the following changes in the Solve_Click() method:

      private function Solve_Click():void
      {
          //request is a array
          Request[0] = 2;
          Request[1] = 2;
          Request[2] = 3;
          serviceProxy.Solve_Send(request);
      }
      

    That is it! So, basically the code above do the following: you added a listener to your service (algorithm class), and the listener is listening for the DrawEvent.DATA_CHANGED event. The DrawEvent.DATA_CHANGED will be dispatched when your client receive a result of the Solve_Send invocation. Thus, the onDataChanged will draw your cube or do whatever you want :)

    The approach above is basic and you have to know how events work in flex and how you can deal with it. Additional information is available here:

    http://livedocs.adobe.com/flex/3/html/help.html?content=createevents_3.html http://livedocs.adobe.com/flex/3/html/help.html?content=events_07.html

    Regards, Cyril