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

标签 java gwtp gwt-platform

我有:

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

目前,我正在将 ChannelEditorPresenter 注入(inject) ChannelPresenter 构造函数,但在本例中,我只有一个 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> {
    }

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

最佳答案

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

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

关于java - GWTP 每次都会创建新的演示者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40774161/

相关文章:

java - 在 JSP 中重定向页面?

java - Apache POI 插入图像

java注释创建所有可能的构造函数

design-patterns - GWT 2.1 中包含的 MVP 框架是否使其他 GWT MVP 框架变得多余?

GWTP 关守和 UserService

java - 使用 hasNextInt 计算整数

java - GWTP(Java): Apply CSS style on div element

java - 泛型 - 继承类修改泛型类型并导致编译器错误

GWT:提供者与异步提供者

javascript - 如何在gwt中处理浏览器事件?