2009-08-04 18 views
2

私は、AsyncTokenの動作方法をActionScriptで理解しようとしています。リモートサービスを呼び出して、特定のパラメータが結果イベント関数またはフォルトイベント関数で使用できることを確認するにはどうすればよいですか?私はそれが私が使用したい非同期機能だと思う。Flex/ActionscriptでAsyncTokenを理解しようとしています

次のコードは、私がしようとしていることをうまく説明します。あなたの説明としてコードブロックを自由に変更してください。

ありがとうございました。

public function testSerivceCall(data:Object, callBackCommand:String):void 
{ 
    // Assume callBackCommand == "FOO"; 
    // How can I pass in callBackCommand as a parameter to the result or fault events? 
    // How do I create an async token here? 

    var remoteObject:RemoteObject; 
    remoteObject = new RemoteObject(); 
    remoteObject.destination = "zend"; 
    remoteObject.source = "MyService"; 
    remoteObject.endpoint = "http://example.com/service"; 
    remoteObject.test.addEventListener(ResultEvent.RESULT, _handleTestResult); 
    remoteObject.test.addEventListener(FaultEvent.FAULT, _handleTestFault); 
    remoteObject.test(data); 
} 

private function _handleTestResult(event:ResultEvent) : void 
{ 
    // How do I get the async token value? 
    // How can I get the value of callBackCommand in this code block? 

    if (callBackCommand == "FOO") 
    { 
     // do something related to "FOO" 
    } 
    else 
    { 
     // do something else with the result event 
    } 


} 

private function _handleTestFault(event:FaultEvent) : void 
{ 
    // How do I get the async token value? 
    // How can I get the value of callBackCommand in this code block? 
} 

この質問は、より明確にする編集:

は、私がどこかに私のコードでは、次のメソッド呼び出しを行うと仮定します

testSerivceCall(personObject, "LoginCommand"); 

は、どのように私は実際の文字列「へのアクセスを得るのですかLoginCommand "_handleTestResult関数ブロック内にありますか?

私はこれをやりたいのは、特定の関数を動的に呼び出して、結果のデータをサービスコールを呼び出す前に知っている特定のコマンドに渡したいからです。

私はちょうどAsyncTokenの構文と機能をgrokkingしています。

答えて

1

リモート呼び出し中に使用されるプロパティ(呼び出しおよび/またはAsycTokenへのパラメータ)にアクセスする場合は、クロージャを使用できます。結果のイベントハンドラは、呼び出し元のメソッド内でクロージャとして定義するだけです。呼び出し関数内の任意の変数にアクセスできます。

public function testSerivceCall(data:Object, callBackCommand:String):void 
{ 
    var _handleTestResult:Function = function(event:ResultEvent) : void 
    { 
     // token is visible here now 
     if (callBackCommand == "FOO") 
     { 
      // do something related to "FOO" 
     } 
     else 
     { 
      // do something else with the result event 
     } 
    } 

    var remoteObject:RemoteObject; 
    remoteObject = new RemoteObject(); 
    remoteObject.destination = "zend"; 
    remoteObject.source = "MyService"; 
    remoteObject.endpoint = "http://example.com/service"; 
    remoteObject.test.addEventListener(ResultEvent.RESULT, _handleTestResult); 
    remoteObject.test.addEventListener(FaultEvent.FAULT, _handleTestFault); 
    var token = remoteObject.test(data); 
} 
+0

これは感謝しています。まだクロージャのハンドルを取得しています。 –

1

質問を正しく読んでいる場合は、ResultEventさんから返された実際のデータにアクセスする方法を見つけようとしていますか?

private function _handleTestResult(event:ResultEvent) : void 
{ 
    // you get the result from the result property on the event object 
    // edit: assuming the class Person exists with a property called name 
    //  which has the value "John" 
    var person : Person = event.result as Person; 

    if (person.name == "John") 
    { 
     Alert.show("John: " + person.name); 
    } 
    else 
    { 
     Alert.show("Not John: " + person.name); 
    } 
} 

private function _handleTestFault(event:FaultEvent) : void 
{ 
    // Maybe you know the type of the returned fault 
    var expectedFault : Object = event.fault as MyPredefinedType 

    if (expectedFault.myPredefinedTypesPredefinedMethod() == "BAR") 
    { 
    // something here 
    } 
} 

ResultEventはのインスタンスを保持する結果と呼ばれる性質を持っている:あなたは正しく呼び出しを行ってきましたし、あなたが期待している形式に戻ってデータを得ていると仮定し

もしそうなら、結果によって返されるオブジェクト(Webサービスを使用している場合はXMLファイルの出力、AMFを使用している場合は直列化されたオブジェクトなど)これはあなたがアクセスしたいものです。同様に、FaultEventには、フォルト情報を返すfaultプロパティがあります。

編集:Gordon Potterのコメントに応じて_handleTestResult()のコードが変更されました。

+0

これは部分的に意味があると思います。しかし、私はより明確に考える必要があります。上記のコードを仮定し、次に私のプロジェクトのどこかで次の関数呼び出しを行うと仮定します。 testSerivceCall(personObject、 "John"); 私のサービス結果の中で、実際の文字列 "John"にアクセスするにはどうすればいいですか? "John"が含まれている人物オブジェクトの場合は、非常に具体的な作業をしたいと思うかもしれません。 これは意味がありますか? –

4

私はクロージャも必要ありませんでした。私は以下のように私が外部的に呼んだクラスを追加しました。受け入れ答えは、元の提出者は、それが実際に頼まれたの質問に答えていない望んで達成しますが

public class MyClass 
{ 
    ... 
    var adminServerRO:AdminServerRO = new AdminServerRO(); 
    adminServerRO.testSerivceCall("FOO",cptyId); 
} 

public class AdminServerRO 
{ 

    private function extResult(event:ResultEvent, token:Object) : void 
    { 
     //the token is now accessed from the paremeter 
     var tmp:String = "in here"; 
    } 

    private function extFault(event:FaultEvent) : void 
    { 
    var tmp:String = "in here"; 
    } 


    public function testSerivceCall(callBackCommand:String, cptyId:String):void 
    { 
     var remoteObject:RemoteObject = new RemoteObject(); 
     remoteObject.destination = "adminServer"; 
     var token:AsyncToken = remoteObject.getCounterpartyLimitMonitorItemNode(cptyId); 
     token.addResponder(new AsyncResponder(extResult,extFault,cptyId)); 
    } 

}

2

呼び出しは、このようなものでした。 AsyncTokenは、リモートメソッド呼び出しの結果として作成され、ResultEventからアクセス可能です。 AsyncTokenは動的クラスなので、任意のプロパティを自由に追加できます。以下のコードは、これを実証する必要があります。

public function testSerivceCall(data:Object, callBackCommand:String):void 
{ 

    var remoteObject:RemoteObject; 
    remoteObject = new RemoteObject(); 
    remoteObject.destination = "zend"; 
    remoteObject.source = "MyService"; 
    remoteObject.endpoint = "http://example.com/service"; 
    remoteObject.test.addEventListener(ResultEvent.RESULT, _handleTestResult); 
    remoteObject.test.addEventListener(FaultEvent.FAULT, _handleTestFault); 
    var token:AsyncToken = remoteObject.test(data); 
    token.callBackCommand = callBackCommand; 
} 

private function _handleTestResult(event:ResultEvent) : void 
{ 

    if (event.token.callBackCommand == "FOO") 
    { 
     // do something related to "FOO" 
    } 
    else 
    { 
     // do something else with the result event 
    } 


} 

private function _handleTestFault(event:FaultEvent) : void 
{ 
     //event.token.callBackCommand should be populated here too 
} 
関連する問題