看看這個命令模式。
當按鈕被按下時,傳遞一個引用到點擊該命令的按鈕。
如果你正在使用Spring作爲,這樣的事情...
<mx:Button id="myButton1" click="EventBus.dispatchEvent(new CommandEvent('testButton', myButton1))" />
在命令中,扔了你的對話,處理響應和設置樣式的引用您在傳遞的按鈕。
這樣可以使你從UI做什麼邏輯組件本身(它甚至不需要是一個按鈕,該命令可以採用UIComponent)。
它也不會在你的應用程序周圍留下狀態,比如最後點擊了哪個按鈕/ etc,因爲你正在執行的邏輯塊的所有狀態都封裝在命令中。
命令的簡要僞
public class TestUICommand implements Command, RequiresContext {
private var _context : UIComponent;
private var _dialog : MyDialog;
// Your command controller would set the reference to the button here.
public function set context(cxt : UIComponent) : void {
_context = cxt;
}
public function execute() : * {
_dialog = new MyDialog();
_dialog.popup();
_dialog.addEventListener("yes", doYes);
_dialog.addEventListener("no", closeDialog);
}
private function doYes(event : Event = null) : void {
_context.setStyle("color", 0xFF0000);
closeDialog();
}
private function closeDialog(event : Event = null) : void {
_dialog.close();
}
}
嗨Amarghosh,謝謝你的幫助。我會遵循你的建議。您的解決方案非常簡單。你說得對,哪個按鈕被點擊只與主應用程序相關。謝謝。 – Laxmidi 2010-10-05 16:30:57