2009-08-04 2 views
2

AsyncToken이 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 함수 블록 안에 있습니까? 나는 동적으로 특정 기능을 다시 호출하고 I가 서비스 호출을하고 때를 미리 알고 특정 명령에 결과 데이터를 손에 원하기 때문에

나는이 작업을 수행 할 수있는 이유입니다.

저는 AsyncToken 구문과 기능을 익히는 데 시간을 보내고 있습니다. 내가 제대로 질문을 읽고 있어요 경우

답변

1

, 당신은 폐쇄를 사용할 수있다. 호출하는 메소드 내부의 결과 이벤트 핸들러를 클로저로 정의하면됩니다. 그런 다음 호출 함수의 모든 변수에 액세스 할 수 있습니다.

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은의 인스턴스를 보유 할 재산이라는 결과가 결과에 의해 반환 된 객체 (예 : 웹 서비스를 사용하는 경우 XML 파일의 출력이거나 AMF를 사용하는 경우 직렬화 된 객체). 이것이 당신이 접근하기 원하는 것입니다. 마찬가지로 FaultEvent에는 오류 정보를 반환하는 fault 속성이 있습니다.

편집 : 고든 포터의 코멘트에 대한 응답으로 _handleTestResult() 코드를 변경. 원격 호출 (호출 및/또는 AsycToken에 매개 변수) 동안 사용 된 속성에 액세스하려면

+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 
} 
관련 문제