在 GWT 中创建模式对话框
我是 GWT 和 Java 新手,试图弄清楚如何在 GWT 中创建同步(模式)对话框,但我遇到了困难。 DialogBox 类有一个标志,表示:
modal - 如果对话框中未包含的小部件的键盘和鼠标事件应被忽略,则为 true
但这并不会使对话框同步。
我读到一项建议,该建议说我需要将处理用户输入的对话框数据的代码放置在“确定”按钮处理程序中。我不喜欢这样,因为它使对话框负责显示数据并处理数据。这导致了糟糕的“关注点分离”,并且违反了良好设计的“单一职责原则”。
我将要执行的代码放在“确定”按钮处理程序中,现在希望在代码中的两个位置使用我的 GWT 对话框。在一种情况下,当按下“确定”按钮时,对话框中的用户数据将添加到表中。在另一种情况下,按下“确定”按钮时表中的数据将被修改。如果我可以创建一个真正同步的对话框,那么当它从执行返回时,我可以处理对话框外部的“确定”按钮,并轻松地重用该对话框。
如果被迫使用我当前的异步模型,我需要按照此处。对于这样一个常见的任务来说,这似乎需要做很多工作。我错过了什么吗?还有其他选择吗?
I'm a GWT and Java newbie trying to figure out how to create a synchronous (modal) dialog in GWT, but I'm having difficulty. The DialogBox class has a flag that says:
modal - true if keyboard and mouse events for widgets not contained by the dialog should be ignored
But that doesn't make the dialog synchronous.
I read one suggestion that said that I need to place the code that processes the dialog's data entered by the user inside the OK button handler. I don't like that because it makes the dialog responsible for displaying the data and processing it too. This results in a poor "Separation of Concerns" and violates the "Single Responsibility Principle" of good design.
I placed the code I want to execute in the OK button handler and now want to have my GWT dialog used in two places in my code. In one case, the user's data from the dialog is added to a table when the OK button is pressed. In the other case, the data in the table is modified when the OK button is pressed. If I could create a truly synchronous dialog, I could handle the OK button outside the dialog when it returns from execution and easily reused the dialog.
If forced to use my current asynchronous model, I'll need to pass in additional data as described here. This seems like a lot of work for such a common task. Did I miss something? Are there other options?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
模态标志基本上使对话框成为 UI 的焦点。因此,除了对话框中的内容之外,无法单击任何其他内容或与之交互。
不幸的是,正如您所知,JavaScript (GWT) 本质上是异步的 - 在浏览器中,只有在用户执行某些操作来触发事件之前,才会执行 JS 代码。所以这是我们必须做出回应的事件。
据我所知,GWT 不提供同步对话框。如果确实如此,则意味着引入了代码的阻塞部分。由于 JavaScript 是单线程的,这会将您的应用程序锁定在繁忙的等待状态 - 这意味着您无法在对话框本身或其他任何地方进行任何有意义的处理。
这就是我处理它的方式:
这与您提到的函数指针链接非常相似 - 您只需编写自己的事件。
The modal flag basically makes the dialog take the focus of the UI. So nothing else can be clicked or interacted with except for what is in the dialog.
Unfortunately as you'll know, JavaScript (GWT) is inherently asynchronous - in the browser there is no JS code executing until the user does something to trigger an event. So it is events we have to respond to.
As far as I am aware GWT does not provide a synchronous dialog. If it did, it would mean that a blocking section of code is introduced. With JavaScript being single threaded, that would lock up your application in busy waiting - meaning that you couldn't do any meaningful processing in the dialog itself or anywhere else for that matter.
This is how I would handle it:
This is very similar to the function pointer link you had mentioned - you'd just be writing your own event.
查看对话框。您可以使用其中一个构造函数使其成为模态的。
Check out DialogBox. You can use one of the constructors to make it modal.
有一种比异步方法更好的方法。
您的应用程序将一直保留,直到用户按“确定”或“取消”为止,您可以处理返回的布尔值。
不幸的是,您无法更改“确定”和“取消”按钮的文本。
There is a better way than the asynchronous approach.
Your application holds until the user presses 'ok' or 'cancel' and you can process the boolean returned.
Unfortunately, you cannot change the text of the 'ok' and 'cancel' buttons.