GWT:在用户在确认框中选择一个选项之前,如何防止处理程序接收事件?
这需要一些解释。
在我们的项目中,我们有一个弹出窗口,其中有很多可以在其中编辑的字段以及底部的“保存”和“取消”按钮。单击“保存”显然会保存您所做的任何更改,或者如果没有更改,则关闭弹出窗口。如果没有任何更改,取消也会关闭弹出窗口。如果您在发生更改时取消,则会弹出一个确认对话框,询问您是否确定要放弃所做的更改。
这个想法是,如果您单击“否”,确认对话框将关闭,但弹出窗口不会关闭,因为用户已表示他们不想放弃这些更改。
现在的问题是,当您单击“取消”按钮时,即使进行了更改,弹出窗口也会立即关闭。确认对话框也会弹出,但没有多大用处,因为弹出窗口已经消失了。
发生这种情况的原因是用于关闭弹出窗口和显示确认对话框的处理程序都作为单击处理程序附加到“取消”按钮。在 GWT 切换到处理程序之前,这曾经有效,因为我们将侦听器列表作为参数传递给显示确认对话框的方法。然后,只有当用户决定可以放弃更改时,我们才会触发这些侦听器。由于事件模型更改后我无法控制处理程序(它们保存在 GWT Widget 代码中),所以我不能再这样做了。
我解决这个问题的想法是简单地将关闭弹出窗口的处理程序添加到另一个事件,只有在确认对话框中选择正确的选项时才会触发该事件。我问这个问题的唯一原因是看看是否有其他方法可以做到这一点,因为添加弹出窗口关闭处理程序的整个事情是在我们的 xml 布局中处理的,并且需要一些有趣的架构更改才能完成这项工作适当地。
:BeforePopupClosedEvent 被触发,打开确认对话框。如果用户指示不应放弃更改,则取消 BeforePopupClosedEvent。不幸的是,代码在等待用户响应时继续执行,并且关闭弹出窗口的处理程序继续执行,因为用户尚未选择选项。
This will take some explaining.
In our project we have a popup which has quite a few fields that can be edited in it as well as Save and Cancel buttons at the bottom. Clicking Save obviously will save any changes that you've made, or just close the popup if there are no changes. Cancel will also close the popup if there are no changes. If you cancel when there are changes, a confirmation dialog pops up asking if you are sure you want to discard the changes you have made.
The idea is that if you click No, the confirmation dialog will close, but the popup won't, since the user has indicated they don't want to discard those changes.
The problem right now is that the popup is closing right away when you click the Cancel button, even if there are changes made. The confirmation dialog also pops up, but it isn't much use since the popup is already gone.
The reason this happens is that the handlers for closing the popup and showing the confirmation dialog are both attached as click handlers to the Cancel button. This used to work before GWT switched to handlers because we passed in the list of listeners as a parameter to the method that showed the confirmation dialog. We would then only fire those listeners if the user decided that it was ok for the changes to be discarded. Since I don't have control over handlers since the event model change (they are held in GWT Widget code) I can't do that anymore.
My idea for fixing this was to simply add the handler that closes the popup to a different event that would only be fired when the correct option is selected in the confirmation dialog. The only reason I'm asking this question is to see if there is any other way to do this because this whole thing where the popup closing handler is added is handled in our xml layouts and it would require some interesting architectural changes to make this work properly.
: BeforePopupClosedEvent is fired which opens the confirmation dialog. If the user indicates the changes should not be discarded, the BeforePopupClosedEvent is cancelled. Unfortunately, code keeps executing while waiting for user response and the handler which closes the popup goes ahead and does that because the user has not selected a choice yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我为解决这个问题而制作的小部件的早期草案。后来我创建了一个更通用的 SimplePanel 包装子类,它将包装任何小部件的点击处理程序。
它的工作原理是在调用 addClickHandler 时切换到替代的 HandlerManager,并且仅在确认对话框返回时沿着该 HandlerManager 触发事件。
This is an early draft of a widget I made to solve this problem. I later made a more generic wrapping subclass of
SimplePanel
that would wrap the click handlers of any widget.It works by switching in a substitute HandlerManager when addClickHandler is called, and only firing events along that HandlerManager when the confirmation dialog returns.