2011-05-25 46 views
0

因此,這裏是我的MXML想獲得工作:如何通過AsyncToken處理遠程方法調用?

<?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"> 

    <fx:Script> 
     <![CDATA[ 
      import argoseye.main.Golem; 

      import mx.collections.ArrayCollection; 
      import mx.rpc.AsyncResponder; 
      import mx.rpc.AsyncToken; 
      import mx.rpc.Responder; 
      import mx.rpc.events.FaultEvent; 
      import mx.rpc.events.InvokeEvent; 
      import mx.rpc.events.ResultEvent; 
      import mx.rpc.remoting.RemoteObject; 
      protected function button1_clickHandler(event:MouseEvent):void 
      { 
       var ro:RemoteObject = new RemoteObject("destination"); 
       ro.endpoint = "http://Jesus/blazeds/messagebroker/amf"; 
       ro.addEventListener(InvokeEvent.INVOKE,onInvoke); 

       var token:AsyncToken = new AsyncToken(); 
       token.addResponder(new AsyncResponder(onResult,onFault)); 

       token = ro.getCells(); 
       textfeld.text = textfeld.text + "Clickhandler called .... \n"; 

      } 

      public function onResult(event:ResultEvent,token:Object):void { 
       textfeld.text = textfeld.text + "Resulthandler called .... \n"; 
       var cellList:ArrayCollection = event.result as ArrayCollection; 

      } 

      public function onFault(event:FaultEvent,token:Object):void 
      { 
       textfeld.text = textfeld.text + "Faulthandler called .... \n"; 
      } 

      public function onInvoke(event:InvokeEvent):void { 
       textfeld.text = textfeld.text + "Invokehandler called .... \n"; 
      } 
     ]]> 
    </fx:Script> 

    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 
    <s:Button x="1093" y="575" label="Button" click="button1_clickHandler(event)"/> 
    <s:TextArea x="1022" y="183" id="textfeld"/> 
</s:Application> 

輸出是

Invokehandler叫....

clickHandler事件稱爲....

Resulthandler不會被調用,雖然BlazeDS控制檯註冊了一個成功的Resultevent。我做錯了什麼?

編輯:我試着將程序導出到一個類,它應該管理這些東西。

package argoseye.main 

{ import flash.events.Event;

import mx.collections.ArrayCollection; 
import mx.messaging.ChannelSet; 
import mx.messaging.channels.AMFChannel; 
import mx.rpc.AsyncResponder; 
import mx.rpc.AsyncToken; 
import mx.rpc.events.FaultEvent; 
import mx.rpc.events.ResultEvent; 
import mx.rpc.remoting.RemoteObject; 

public class Schem 
{ 
    public var info:String=""; 


    public function Schem() 
    {  
    } 

    public function loadCurrentSchem():void 
    { 
     var ro:RemoteObject = new RemoteObject("Hibernatetest"); 
     ro.endpoint = "http://jesus/blazeds/messagebroker/amf"; 

     var token:AsyncToken = ro.getCells(); 
     token.addResponder(new AsyncResponder(onResult,onFault)); 

     info = info + "Loader Called ..."; 


    } 

    public function onResult(event:ResultEvent,token:Object):void { 
     var cellList:ArrayCollection = event.result as ArrayCollection; 
     info = info + "Resulthandler Called"; 

    } 

    public function onFault(event:FaultEvent,token:Object):void 
    { 

    } 
    //Eventhandlers 


    //Getters, Setters 


} 

}

如果我叫它,它不到達事件處理程序。我的錯誤在哪裏?

回答

5

你的錯誤就在於這些行:

var token:AsyncToken = new AsyncToken(); 
token.addResponder(new AsyncResponder(onResult,onFault)); 
token = ro.getCells(); 

你在第1行創建一個新的令牌。 您在第2行指定了響應者。 然後你重新分配第3行的令牌。

你在第3行做的是有效地創建一個新的令牌,因此它沒有附加響應者,因爲它是一個新的實例。

所以它應該是:

var token:AsyncToken = ro.getCells(); 
//ro.getCells() will return a new instance of AsyncToken 
token.addResponder(new AsyncResponder(onResult,onFault)); 
+0

非常有啓發!謝謝。我不清楚'token = ro.getCells();'的深層含義。 – Kai 2011-05-25 12:56:53

2

使用下面的代碼:

<fx:Script> 
    <![CDATA[ 
     import argoseye.main.Golem; 

     import mx.collections.ArrayCollection; 
     import mx.rpc.AsyncResponder; 
     import mx.rpc.AsyncToken; 
     import mx.rpc.Responder; 
     import mx.rpc.events.FaultEvent; 
     import mx.rpc.events.InvokeEvent; 
     import mx.rpc.events.ResultEvent; 
     import mx.rpc.remoting.RemoteObject; 
     protected function button1_clickHandler(event:MouseEvent):void 
     { 
      var ro:RemoteObject = new RemoteObject("destination"); 
      ro.endpoint = "http://Jesus/blazeds/messagebroker/amf"; 
      ro.addEventListener(InvokeEvent.INVOKE,onInvoke); 

      var token:AsyncToken = ro.getCells(); 
      token.addResponder(new AsyncResponder(onResult,onFault)); 
      textfeld.text = textfeld.text + "Clickhandler called .... \n"; 

     } 

     public function onResult(event:ResultEvent,token:Object):void { 
      textfeld.text = textfeld.text + "Resulthandler called .... \n"; 
      var cellList:ArrayCollection = event.result as ArrayCollection; 

     } 

     public function onFault(event:FaultEvent,token:Object):void 
     { 
      textfeld.text = textfeld.text + "Faulthandler called .... \n"; 
     } 

     public function onInvoke(event:InvokeEvent):void { 
      textfeld.text = textfeld.text + "Invokehandler called .... \n"; 
     } 
    ]]> 
</fx:Script> 

<fx:Declarations> 
    <!-- Place non-visual elements (e.g., services, value objects) here --> 
</fx:Declarations> 
<s:Button x="1093" y="575" label="Button" click="button1_clickHandler(event)"/> 
<s:TextArea x="1022" y="183" id="textfeld"/> 

+0

它的工作。但是,如何調用構造函數有害? – Kai 2011-05-25 11:37:33

+0

我認爲問題是由ro.getCells()函數返回的AsyncToken與您通過new創建的AsyncToken對象完全不同。因此,事件偵聽器不會添加到令牌中,並且您無法獲得正確的行爲。 – phtrivier 2011-05-25 11:40:32

+0

@phtrivier絕對! :) – Constantiner 2011-05-25 13:49:35