Pavel Sokolov
Pavel Sokolov

Reputation: 71

GWTP create new presenter each time

I have:

Currently I'm injecting ChannelEditorPresenter to ChannelPresenter constructor, but in this case I have only one instance of the ChannelEditorPresenter. Actually I need separate Popup presenter for each call. (A lot of separated windows, each has own data).

ChannelPresenter.java:

public class ChannelPresenter extends Presenter<ChannelPresenter.MyView, ChannelPresenter.MyProxy> implements ChannelUiHandlers {

    public interface MyView extends View, HasUiHandlers<ChannelUiHandlers> {
        void load();
    }

    @ProxyStandard
    @NameToken(NameTokens.CHANNELS)
    interface MyProxy extends ProxyPlace<ChannelPresenter> {
    }

    ChannelEditorPresenter channelEditorPresenter;

    @Inject
    ChannelPresenter(EventBus eventBus, MyView view, MyProxy proxy,
                     ChannelEditorPresenter channelEditorPresenter
                     ) {
        super(eventBus, view, proxy, ApplicationPresenter.SLOT_MAIN);
        getView().setUiHandlers(this);
        this.channelEditorPresenter = channelEditorPresenter;
    }

    @Override
    protected void onBind() {
        super.onBind();
        getView().load();
    }

    @Override
    public void displayEditor(Channel channel) {
        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        //  Here I need to create new instance for each call
        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        addToPopupSlot(channelEditorPresenter);
        channelEditorPresenter.edit(channel);
    }
}

Upvotes: 1

Views: 311

Answers (2)

Jake W
Jake W

Reputation: 2858

We had the same issue recently, and we found the best way is to use WidgetsFactory with @Assisted annotation, as mentioned in a Arcbees' blog post: http://blog.arcbees.com/2015/04/01/gwt-platform-event-best-practices-revisited/

This is useful especially when you need to pass different parameters to the constructor of the presenter widget.

Upvotes: 0

Pavel Sokolov
Pavel Sokolov

Reputation: 71

I found solution here: Instantiate a PresenterWidget (GWTP) manually

I need to Inject com.google.inject.Provider<ChannelEditorPresenter> instead of plain ChannelEditorPresenter.

ChannelPresenter.java:

public class ChannelPresenter extends Presenter<ChannelPresenter.MyView, ChannelPresenter.MyProxy> implements ChannelUiHandlers {

    public interface MyView extends View, HasUiHandlers<ChannelUiHandlers> {
        void load();
    }

    @ProxyStandard
    @NameToken(NameTokens.CHANNELS)
    interface MyProxy extends ProxyPlace<ChannelPresenter> {
    }

    Provider<ChannelEditorPresenter> channelEditorPresenterProvider;

    @Inject
    ChannelPresenter(EventBus eventBus, MyView view, MyProxy proxy,
                     Provider<ChannelEditorPresenter> channelEditorPresenterProvider
                     ) {
        super(eventBus, view, proxy, ApplicationPresenter.SLOT_MAIN);
        getView().setUiHandlers(this);
        this.channelEditorPresenterProvider = channelEditorPresenterProvider;
    }

    @Override
    protected void onBind() {
        super.onBind();
        getView().load();
    }

    @Override
    public void displayEditor(Channel channel) {
        ChannelEditorPresenter channelEditorPresenter = channelEditorPresenterProvider.get();
        addToPopupSlot(channelEditorPresenter);
        channelEditorPresenter.edit(channel);
    }
}

Upvotes: 1

Related Questions