WelcomeTo
WelcomeTo

Reputation: 20571

GWT MVP. How to use event bus?

In project I use MVP pattern. I have 2 view and 2 corresponding presenters. From "Presenter2" i want to get selected value in "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).

Upvotes: 0

Views: 689

Answers (1)

steffinchen
steffinchen

Reputation: 497

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:

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(); and then you can just call a method from the view which returns the selected value.

Upvotes: 1

Related Questions