Search code examples
gwtgwtpgwt-platform

non-singleton PresenterWidgets/Views in GWTP project


I'm using GWT with the GWTP framework in my projects. Until now all presenters/views where of type singleton (one dedicated window for each implementation type). Now i have a special (document) window where i want to create a new presenter/view (window) for each document the user wants to open.

GWTP class com.gwtplatform.mvp.client.gin.AbstractPresenterModule contains methods for binding non-singleton PresenterWidgets/Views, for example with a PresenterWidget factory;

enter image description here

But i cannot find any documentation or examples about this GWTP PresenterWidget factory usages. How do i implement this PresenterWidget factories?


Solution

  • There's a difference between using com.gwtplatform.mvp.client.PresenterWidget and com.gwtplatform.mvp.client.Presenter when implementing your own presenter classes.

    When using Presenter (with a PresenterProxy), GWTP handles the presenter as singleton.

    public class MyPresenter extends Presenter<MyPresenter.MyView, MyPresenter.MyProxy>
    

    When using PresenterWidget the presenter will be instantiated multiple times (like a Spring prototype scope)

    public class MyPresenter extends PresenterWidget<MyPresenter.MyView>
    

    Then use com.google.inject.Provider get() to instantiate the presenter. When using PresenterWidget it will result in multiple instances. When using Presenter the singleton presenter is returned. For example:

    @Inject
    MySecondPresenter(EventBus eventBus, MySecondView view, MySecondProxy proxy, Provider<MyPresenter> myProvider) {
        super(eventBus, view, proxy, RevealType.Root);
        setMyProvider(myProvider);
    }
    

    and

    MyPresenter p = getMyProvider().get();
    getView().addMyPresenter(p);
    

    Hope this helps