MVP 和 GWT 小部件之间的通信 [英] MVP and communication between GWT widgets

查看:24
本文介绍了MVP 和 GWT 小部件之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将 MVP 模式与 GWT 一起使用,就像在 2009 年 Google I/O 的 GWT 架构最佳实践演讲中一样,但已将信息分散到多个小部件中,那么应该如何填充值对象?

If I am using the MVP pattern with GWT, as in the GWT architecture best practices talk from Google I/O from 2009, but have spread out the information into multiple widgets, how should the value object be populated?

假设我有一个 EditPersonView/Presenter、一个 EditPetView/Presenter 和一个 EditAddressView/Presenter,最后两个是作为 EditPersonView 面板一部分的小部件.有了这些,我有以下课程:

Say I have a EditPersonView/Presenter, a EditPetView/Presenter and an EditAddressView/Presenter and the last two are widgets as a part of a panel in the EditPersonView. With these I have the following class:

class PersonDetails {
    private PetDetails pet;
    private AddressDetails addressDetails;

    // ...
}

PetDetails 和 AddressDetails 实例变量在它们的 Presenter 副本中进行管理.当用户点击 EditPersonView 中的保存"按钮时,widget 之间应该如何进行通信,以便 PersonDetails 填充来自其子 widget 的信息?

The PetDetails and AddressDetails instance variables are managed in their presenter counterparts. When the user clicks the "Save" button in the EditPersonView, how should the communication between the widgets be done so that the PersonDetails is filled with information from its child widgets?

推荐答案

我在使用 Ray Ryan 的方法设计的几个不同的 GWT 应用程序中遇到了同样的问题.我的首选解决方案是创建一个单例会话对象",用于存储应用程序该部分的状态.在您的示例中,它可能如下所示:

I've faced this same problem in a few different GWT applications that I've designed using Ray Ryan's approach. My preferred solution is to create a Singleton "session object" that stores the state of that part of the application. In your example, it might look like this:

interface EditPersonSession {

    void fetchPerson(PersonId id);
    PersonDetails getCurrentPersonDetails();
    void updatePersonDetail(PersonDetail<?> detail);
    void updatePetDetail(PetDetail<?> detail);
    void updateAddressDetail(AddressDetail<?> detail);
    void save();

}

所有三个演示者都包含对会话对象的引用(可能由 Gin 注入).每当用户操作 UI(视图)时,与该视图关联的演示者立即将状态推送到共享会话对象.例如,在 EditAddressPresenter 中:

All three presenters contain a reference to the session object (perhaps injected by Gin). Whenever the UI (view) is manipulated by the user, the presenter associated with that view immediately pushes the state to the shared session object. For example, inside EditAddressPresenter:

view.getStreetNameTextBox().addValueChangeHandler(new ValueChangeHandler() {

    void onValueChange(ValueChangeEvent<String> event) {
        editPersonSession.updateAddressDetail(new StreetNameAddressDetail(event.getValue()));
    }

}

当需要保存时,状态对象被告知将状态保存到服务器.此时,会话对象具有数据的最新表示,并且可以一次性保存所有数据.因此,在 EditPersonPresenter 中:

When it is time to save, the state object is told to save the state to the server. At this point, the session object has up-to-date representations of the data, and can save it all at once. So, in EditPersonPresenter:

view.getSaveButton().addClickHandler(new ClickHandler() {

    void onClick(ClickEvent event) {
        editPersonSession.save();
    }

}

这样,演示者之间不需要包含任何相互引用,但可以向服务器发送一致的信息.如果演示者需要知道他们显示的信息何时更新(由其他演示者或服务器更新),会话对象可以通过在事件总线(共享单例处理程序管理器)上触发事件来通知他们.然后演示者可以从会话对象中提取最新的 PersonDetails.

This way, the presenters need not contain any references to each other, but can send consistent information to the server. If the presenters need to know when information that they display has been updated (either by other presenters, or by the server), the session object can notify them by firing events on the event bus (shared Singleton HandlerManager). The presenters can then pull the most current PersonDetails from the session object.

这篇关于MVP 和 GWT 小部件之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆