1

这里是 GWT 的新手...

我正在使用 UIBinder 方法来布局应用程序,有点类似于 GWT 邮件示例的风格。该应用程序以在 onModuleLoad() 方法中添加到 RootLayoutPanel 的 DockLayoutPanel 开始。DockLayoutPanel 有一个静态的 North 和一个静态的 South,使用自定义的中心小部件定义如下:

public class BigLayoutWidget extends ResizeComposite {
...
}

此自定义小部件使用 BigLayoutWidget.ui.xml 进行布局,而 BigLayoutWidget.ui.xml 又包含一个 TabLayoutPanel(3 个选项卡),其中第一个包含一个 SplitLayoutPanel,分为 WES​​T (Shortcuts.ui.xml) 和 CENTER (Workpanel.ui.xml )。反过来,快捷方式由具有 3 个堆栈的 StackLayoutPanel 组成,每个堆栈都在其自己的 ui.xml 文件中定义。

我希望在快捷方式的单个堆栈之一中单击事件来更改工作面板的内容,但到目前为止,我只能在同一类中操作小部件。使用最简单的情况,我无法在快捷方式中单击按钮来清除 Workpanel 的内容或使 WorkPanel 不可见。

几个问题...

  1. ResizeComposite 是为此扩展的正确类型吗?我正在遵循 TopPanel、MailList 等邮件示例中的方法,所以可能不是?
  2. 如何使这些点击操作它们不驻留的面板的内容?
  3. 是否不再建议使用侦听器来处理事件?我想我在编译期间的某个地方看到这些天使用了 ClickHandler,并且不推荐使用单击侦听器“订阅”方法(我主要使用 @UiHandler 注释)
  4. 有没有一种简单的方法来处理我的应用程序/页面中的特定元素?(在 UI.XML 文件中应用“ID”字段会生成弃用警告)。我正在寻找类似 document.getElementById() 的东西,它可以让我处理特定元素。如果存在,如何在元素上设置句柄/ID,然后如何按名称/ID 调用该元素?

请注意,我的布局本身非常好;这是我无法完全理解的从一个 ui.xml 模块化面板到下一个的交互。

提前致谢。

4

1 回答 1

4
  1. If you don't have a use for resizing events than just use Composite
  2. What you want is what the GWT devs called message bus (implemented as HandlerManager). You can get a nice explanation in the widely discussed (for example, on the GWT Google Group, just search for 'mvp') presentation by Ray Ryan from Google I/O 2009 which can be found here. Basically, you "broadcast" an event on that message bus and then a Widget listening for that event gets the message and does its stuff.
  3. Yep, *Handlers are the current way of handling events - the usage is basically the same so migration shouldn't be a problem (docs). They changed it so that they could introduce custom fields in the future, without breaking existing code.
  4. If you've set an id for any DOM element (for Widgets I use someWidget.getElement().setId(id), usually in combination with DOM.createUniqueId()) you can get it via GWT.get(String id). You'll get then a RootPanel which you'll have to cast to the right Widget class - as you can see it can get a little 'hackish' (what if you change the type of the Widget by that id? Exceptions, or worse), so I'd recommend sticking with MVP (see the first point) and communicating via the message bus. Remember however, that sometimes it's also good to aggregate - not everything has to be handled via the message bus :)

Bottom line is I'd recommend embracing MVP (and History) as soon as possible - it makes GWT development much easier and less messy :) (I know from experience, that with time the code starts to look like a nightmare, if you don't divide it into presentation, view, etc.)

于 2010-02-18T23:39:25.050 回答