GWTP每次都会创建新的演示者

时间:2016-11-23 21:06:58

标签: java gwtp gwt-platform

我有:

  • 简单嵌套演示者(ChannelPresenter),其中包含表(网格),其中包含记录。我需要在每个ChannelPresenter.displayEditor()调用中创建新的ChannelEditorPresenter实例。
  • Popup Presenter Widget(ChannelEditorPresenter),应在每个ChannelEditorPresenter.edit()调用中显示弹出窗口

目前我正在将ChannelEditorPresenter注入ChannelPresenter构造函数,但在这种情况下,我只有一个ChannelEditorPresenter实例。实际上我需要为每个电话分别设置Popup演示者。 (很多独立的窗口,每个都有自己的数据)。

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

2 个答案:

答案 0 :(得分:0)

我在这里找到了解决方案:Instantiate a PresenterWidget (GWTP) manually

我需要注入 com.google.inject.Provider&lt; ChannelEditorPresenter&gt; ,而不是普通的 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);
    }
}

答案 1 :(得分:0)

我们最近遇到了同样的问题,我们发现最好的方法是使用带有@Assisted注释的WidgetsFactory,如Arcbees的博文中所述:http://blog.arcbees.com/2015/04/01/gwt-platform-event-best-practices-revisited/

当您需要将不同的参数传递给演示者窗口小部件的构造函数时,这非常有用。