2011-03-13 61 views
0

我有一個在Parent中調度事件的子組件。父母的事件打電話給我們的數據庫。目前,該事件被解僱&孩子繼續沒有結果。我該如何做到讓孩子等待孩子繼續學習數據庫的結果?如何等待事件完成

兒童

<fx:Script> 
<![CDATA[ 
dispatchEvent(new Event("getDBcontents")); // dispatch the event in the parent 

// do some more stuff here but we need pause until we get the result from the parent 

    ]]> 
</fx:Script> 
在父母

public function getDBcontents(event:Event):void { 

otherChild.getResult.token = otherChild.childRet.getContents('userID.text'); 

} 

回答

0

移動「//做一些更多的東西在這裏,但我們需要暫停,直到我們得到來自母公司的結果」一節一不同的部分。我假設你正在對你的數據庫進行一個遠程調用,它有一個回調。我不確定你正在使用哪種機制,但讓我們假設一個RemoteObject。

您可以在發送的自定義事件上傳遞一個函數。您的代碼的數據庫部分可以將該函數指針附加到AsyncToken,或者將其添加到類實例中。然後,當它返回結果時,您可以調用您作爲事件一部分傳入的函數。異步編程的樂趣。

我建議您查看Cairngorm和Swiz(Swiz是我的首選框架)中使用的模式,因爲他們在這些框架中執行數據庫調用的方式正是您在此嘗試執行的操作。

舉個例子,你可以做這樣的事情:

dispatchEvent(new MyCustomEvent("getDBcontents", callBackFunction)); 

private function callBackFunction(stuffToProcess:Object):void { 
    //do more stuff here after the stuff is returned 
} 


//first create MyCustomEvent class extending Event 

//Then you need something to handle the event, you can build the event listener yourself, or use something like Swiz to make your life easier 

//here is your event handler that you can call yourself, or assign through Swiz Cairngorm 

var st:Function; 
public myEventHandler(event:MyCustomEvent):void { 
    st = event.callBackFunction; //your param on your custom function 
    var token:AsyncToken=this.service.doSomething(); 
    var responder:mx.rpc.Responder=new mx.rpc.Responder(genericResultsHandler, faultHandler); 
    token.addResponder(responder); 
} 

genericResultsHandler(result:ResultEvent):void{ 
    if (st != null) 
    st(result.data); 
} 
+0

我使用PHP服務瓦特/回調。 「然後,當它返回結果時,你可以調用你作爲事件一部分傳入的函數。」...我該怎麼做? – 2011-03-13 16:55:24

+0

我翻遍Swiz和Cairngorm,但無法很好地遵循。沒有辦法「強制」我的腳本暫停,直到我從數據庫中得到結果爲止? – 2011-03-13 17:19:25

+0

不,沒有辦法讓你的代碼以你想要的方式「暫停」。網絡調用在actionscript中是異步的,因此您需要提供回調機制。這個回調機制爲您提供了您正在尋找的僞「暫停」。我強烈建議回去看看Swiz,因爲它可以幫助您輕鬆地組織您正在嘗試執行的任務。 – Scott 2011-03-13 21:08:17