看看这个命令模式。
当按钮被按下时,传递一个引用到点击该命令的按钮。
如果你正在使用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