2009-11-12 57 views
0

我需要从我的Responder对象返回值。现在,我有:从Flex/ActionScript 3 Responder对象返回

private function pro():int { 
    gateway.connect('http://10.0.2.2:5000/gateway'); 
    var id:int = 0; 
    function ret_pr(result:*):int { 
     return result 
    } 
    var responder:Responder = new Responder(ret_pr); 
    gateway.call('sx.xj', responder); 
    return id 
} 

基本上,我需要知道如何让ret_pr的返回值到ID或任何东西,我从该函数返回。响应者似乎吃了它。我不能在其他地方使用公共变量,因为它会一次运行多次,所以我需要本地范围。

回答

2

这就是我如何写AMF服务器的连接,调用它并存储结果值。请记住,结果不会立即可用,因此您将设置响应者在数据从服务器返回后“回复”数据。

public function init():void 
{ 
    connection = new NetConnection(); 
    connection.connect('http://10.0.2.2:5000/gateway'); 
    setSessionID(1); 
} 
public function setSessionID(user_id:String):void 
{ 
    var amfResponder:Responder = new Responder(setSessionIDResult, onFault); 
    connection.call("ServerService.setSessionID", amfResponder , user_id); 
} 

private function setSessionIDResult(result:Object):void { 
    id = result.id; 
    // here you'd do something to notify that the data has been downloaded. I'll usually 
    // use a custom Event class that just notifies that the data is ready,but I'd store 
    // it here in the class with the AMF call to keep all my data in one place. 
} 
private function onFault(fault:Object):void { 
    trace("AMFPHP error: "+fault); 
} 

我希望能指出你在正确的方向。

0
private function pro():int { 
    gateway.connect('http://10.0.2.2:5000/gateway'); 
    var id:int = 0; 
    function ret_pr(result:*):int { 
     return result 
    } 
    var responder:Responder = new Responder(ret_pr); 
    gateway.call('sx.xj', responder); 
    return id 
} 

此代码永远不会让你得到你想要的。您需要使用正确的结果函数。匿名函数responder返回值不会被周围的函数使用。在这种情况下它将总是返回0。您正在处理异步调用,并且您的逻辑需要相应处理。

private function pro():void { 
    gateway.connect('http://10.0.2.2:5000/gateway'); 
    var responder:Responder = new Responder(handleResponse); 
    gateway.call('sx.xj', responder); 
} 

private function handleResponse(result:*):void 
{ 
    var event:MyCustomNotificationEvent = new MyCustomNotificationEvent( 
      MyCustomNotificationEvent.RESULTS_RECEIVED, result); 
    dispatchEvent(event); 
    //a listener responds to this and does work on your result 
    //or maybe here you add the result to an array, or some other 
    //mechanism 
} 

使用匿名函数/闭包的关键是不会给你某种伪同步行为。