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

问题描述

如果我使用与GWT MVP模式,如GWT的架构最佳实践从谷歌I / O谈从2009年,但小号$ P $垫出来的信息分成多个小部件,值对象应该如何填充?

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保存按钮,应该如何小部件之间的通信来完成,以使PersonDetails从其子部件充满信息?

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?

推荐答案

我在,我已经使用雷瑞安的做法设计了几个不同的GWT应用程序面临同样的问题。
我的preferred的解决方案是创建一个存储应用程序的那部分状态的单身会话对象。在您的例子,它可能是这样的:

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();

}

这三个presenters包含对会话对象(可能由杜松子酒注入)的参考。每当用户界面(视图)是由用户操纵,与该视图相关联的presenter立即推状态到共享会话对象。例如,内EditAddress presenter:

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()));
    }

}

当它是时间来保存,状态对象被告知状态保存到服务器上。在这一点上,会话对象具有数据的最高最新重新presentations,并且可以在一次保存这一切。所以,在EditPerson presenter:

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();
    }

}

此方式,presenters不必含有彼此的任何引用,但是可以发送一致的信息给服务器。
如果presenters需要知道什么时候它们显示信息已经被更新(或者通过其他presenters,或由服务器),会话目标可通过触发事件总线上的事件通知他们(共享的Singleton HandlerManager) 。然后,presenters可以从会话对象中获取最新的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小部件之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 08:47