简体   繁体   English

GWT MVP。 如何使用事件总线?

[英]GWT MVP. How to use event bus?

In project I use MVP pattern. 在项目中,我使用MVP模式。 I have 2 view and 2 corresponding presenters. 我有2个视图和2个相应的演示者。 From " Presenter2 " i want to get selected value in " View1 ". 我想从“ Presenter2 ”中获取“ View1 ”中的选定值。 What is the best way to do it? 最好的方法是什么? I know that better to use event bus. 我知道最好使用事件总线。 But so i must to create 2 events and 2 event handlers (1st event will rise when Presenter2 need selected value from View1 , and it will be handled in Presenter1. 2nd event will rise in Presenter1 (like: new selectedValueEvent(value) to notificate Presenter2 about selected value. So Presenter2 will handle selectedValueEvent(value) and get value ). 但是,所以我必须创建2个事件和2个事件处理程序(当Presenter2需要从View1选择值时,第一个事件将上升,并将在Presenter1.处理Presenter1.第二个事件将在Presenter1上升(例如: new selectedValueEvent(value)来通知Presenter2)关于选定值。因此Presenter2将处理selectedValueEvent(value)并获取value )。

If the point when the presenter needs to get the selected value is when the user makes an action you won't get around using an event. 如果演示者需要获取所选值的时间点是在用户执行操作时,您将无法使用事件。 (Altough, maybe both presenters could react to the same event so don't need to use two different ones?) (尽管如此,也许两个演示者都可以对同一事件做出反应,所以不需要使用两个不同的演示者吗?)

If it is known when the presenter needs to get the value (a defined step in a workflow), you could to it like this: 如果知道演示者何时需要获取值(工作流中已定义的步骤),则可以这样:

Keep a reference to the views in the ClientFactory : 保留对ClientFactory视图的ClientFactory

public class ClientFactoryImpl implements ClientFactory {
    private static final EventBus eventBus = new SimpleEventBus();
    /* The views */
    private static final SampleView sampleView = new SampleView(); 
    ....

    public ClientFactoryImpl(){
        eventBus.addHandler(ReleaseAddedEvent.type, sampleView);
        ....
    }


    // getter and setters
}

So in the Presenter you can get a reference to the view: SampleView view = MyEntryPoint.getClientFactory().getSampleView(); 因此,在Presenter中,您可以获取对该视图的引用: SampleView view = MyEntryPoint.getClientFactory().getSampleView(); and then you can just call a method from the view which returns the selected value. 然后您可以从视图中调用一个返回所选值的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM