2012-01-20 38 views
0

我在Flex應用程序中需要創建&從調用返回URL填充對象。這裏是我需要做的:需要處理來自URLLoader.load調用的返回字符串

  1. 我有一個類與Web服務器通信。
  2. 我在這個類(稱爲getPerson)中有一個函數,它將返回一個Person對象,該對象從Web服務器返回的XML數據中填充。

我遇到的問題(似乎這是一個非常普遍的問題,但我沒有看到一個解決方案,我可以看到會工作)是URLLoader的加載方法是異步的。

我有一個Event.COMPLETE事件觸發Event.COMPLETE事件,它解析XML並在事件處理程序中填充我的對象,但是如何將此對象返回到我的應用程序中最初稱爲getPerson函數的原始代碼?

因此,當從服務器返回時,我的方法完成,我不能返回我填充的Person對象。

我的問題是我該如何做到這一點?對於ActionScript我還是比較新的,現在我已經在這一天旋轉了我的輪子。

我添加這證明我有這個問題的一些示例代碼 - 我已經簡化了,我使用的是什麼:

MXML應用程序文件:

<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
          xmlns:s="library://ns.adobe.com/flex/spark" 
          xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" 
          creationComplete="application1_creationCompleteHandler(event)"> 
    <fx:Script> 
     <![CDATA[ 
      import mx.events.FlexEvent; 

      protected function application1_creationCompleteHandler(event:FlexEvent):void 
      { 
       var d:DAL = new DAL(); 
       d.CreateNewPerson("John Smith"); 
      } 

     ]]> 
    </fx:Script> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 

</s:Application> 

DAL.cs文件:

package 
{ 
    import flash.events.Event; 
    import flash.net.URLLoader; 
    import flash.net.URLRequest; 
    import flash.net.URLRequestMethod; 

    import mx.controls.Alert; 

    public class DAL 
    { 
     public function DAL() 
     { 
     } 

     public function CreateNewPerson(Name:String):void 
     { 

      var strXML:String = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; 
      var loader:URLLoader = new URLLoader(); 
      loader.addEventListener(Event.COMPLETE, onPostComplete); 
      var request:URLRequest = new URLRequest("http://www.cnn.com"); 
      request.method = URLRequestMethod.POST; 
      request.data = strXML; 
      loader.load(request); 
     } 

     private function onPostComplete(evt:Event):void 
     { 
      //Process returned string 

      //Here is where I need to return my object 
        var obj:Object = new Object() 
     } 
    } 
} 

我需要做的是以某種方式將「obj」變量返回給我的MXML應用程序文件,以便我可以在那裏使用它。

在此先感謝!

+0

如果發佈一些代碼會有所幫助。在附註中,爲什麼不將代碼從源代碼移到Event.COMPLETE事件? – Angelo

回答

1

在您的MXML應用程序文件:

var d:DAL = new DAL(); 

protected function application1_creationCompleteHandler(event:FlexEvent):void 
{ 
    d.CreateNewPerson("John Smith"); 
    d.addEventListener('PersonCreated', personCreated); 
} 

private function personCreated(evt:Event) :void 
{ 
    var obj:Object = new Object(); 
    obj = d.ojectToBeReturned; 
    // obj will contain the object from your class... 
} 

在您的DAL班級,十二月LARE對象變量和創建的getter/setter函數,即

private var _myObjectToBeReturned:Object; 

public function get ojectToBeReturned() :Object 
{ 
    return _myObjectToBeReturned; 
} 

並在您的方法

private function onPostComplete(evt:Event):void 
{ 
    //Here is where I need to return my object 
    _myObjectToBeReturned = new Object(); 
    // Perform the process for the object. 

    // Call the event from your parent. 
    this.dispatchEvent(new Event('PersonCreated')); 
} 
+0

非常感謝!它像一個魅力! – Nutkraker

0

主MXML文件

<fx:Script> 
    <![CDATA[ 
     import flash.events.Event; 
     import mx.events.FlexEvent; 

     protected function application1_creationCompleteHandler(event:FlexEvent):void 
     { 
      var d:DAL = new DAL(); 
      d.addEventListener(Event.COMPLETE, onLoadComplete); 
      d.CreateNewPerson("John Smith"); 
     } 

     private function onLoadComplete(e:Event):void 
     { 
      trace("DATA LOADED") 
     } 
    ]]> 
</fx:Script> 

和你DAL.as

package 
    { 
    import flash.events.Event; 
    import flash.events.EventDispatcher; 
    import flash.events.IEventDispatcher; 
    import flash.events.IOErrorEvent; 
    import flash.events.SecurityErrorEvent; 
    import flash.net.URLLoader; 
    import flash.net.URLRequest; 
    import flash.net.URLRequestMethod; 
    import mx.controls.Alert; 
    /** 
    * ... 
    * @author Jeet Chauhan 
    */ 
    public class DAL implements IEventDispatcher 
    { 
     private var dispatcher:IEventDispatcher; 

     public function DAL() { 
      dispatcher = new EventDispatcher(); 
     } 

     public function CreateNewPerson(Name:String):void { 
      /*var strXML:String = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; 
      var loader:URLLoader = new URLLoader(); 
      loader.addEventListener(Event.COMPLETE, onPostComplete); 
      loader.addEventListener(IOErrorEvent.IO_ERROR, onPostComplete); 
      loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onPostComplete); 
      var request:URLRequest = new URLRequest("http://www.cnn.com"); 
      request.method = URLRequestMethod.POST; 
      request.data = strXML; 
      loader.load(request);*/ 

      // let assume data loaded and onPostComplete called 
      onPostComplete(new Event(Event.COMPLETE)); 
     } 



     private function onPostComplete(evt:Event):void { 
      //Process returned string 

      //Here is where I need to return my object 
      var obj:Object = new Object() 

      dispatchEvent(evt); 
     } 


     /* INTERFACE flash.events.IEventDispatcher */ 

     public function dispatchEvent(event:Event):Boolean 
     { 
      return dispatcher.dispatchEvent(event); 
     } 

     public function hasEventListener(type:String):Boolean 
     { 
      return dispatcher.hasEventListener(type); 
     } 

     public function willTrigger(type:String):Boolean 
     { 
      return dispatcher.willTrigger(type); 
     } 

     public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void 
     { 
      dispatcher.removeEventListener(type, listener, useCapture); 
     } 

     public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void 
     { 
      dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference); 
     } 


    } 
} 

希望這有助於

+0

嗨Jeetendra - 你能通過修改我添加到我原來的問題中的示例代碼來告訴我你的意思嗎?我明白你的意思,但我不知道如何在AS3中做到這一點......謝謝! – Nutkraker

相關問題