GWT,如何使用HandlerManager中的EventBus从小部件或组合中触发事件

时间:2016-07-28 11:32:17

标签: events gwt event-handling gwtp gwt-platform

我有小工具。我想发布如下事件:

fireEvent(new IndicatorStartEvent("Message"));

但它不起作用。

通常我会使用Presenter(GWTP),但现在我只想拥有常规小部件:

public class FileUploadWidget extends Composite {
    MaterialFileUploader uploader = new MaterialFileUploader();

    @Inject
    public FileUploadWidget(String triggerId, EventBus eventBus) {
        super();
        initWidget(uploader);
        Window.alert("TEST Start");
        fireEvent(new IndicatorStartEvent("Message"));
    }
}

这是事件代码:

public class IndicatorStartEvent extends GwtEvent<IndicatorStartEvent.IndicatorHandler> {
    public static Type<IndicatorHandler> TYPE = new Type<IndicatorHandler>();

    public interface IndicatorHandler extends EventHandler {
        void onIndicatorProgressStart(IndicatorStartEvent event);    
    }

    public interface IndicatorHandlers extends HasHandlers {
        HandlerRegistration addStartIndicatorHandler(IndicatorHandler handler);
    }


    private final String message;

    public IndicatorStartEvent(final String message) {
        this.message = message;
    }

    public static Type<IndicatorHandler> getType() {
        return TYPE;
    }

    @Override
    protected void dispatch(final IndicatorHandler handler) {
        handler.onIndicatorProgressStart(this);
    }

    @Override
    public Type<IndicatorHandler> getAssociatedType() {
        return TYPE;
    }

    public String getMessage() {
        return this.message;
    }    
}

这是我的应用演示者处理事件:

public class AppPresenter extends TabContainerPresenter<AppPresenter.MyView, AppPresenter.MyProxy> implements AppUiHandlers
, IndicatorStartEvent.IndicatorHandler {
    @ProxyStandard
    public interface MyProxy extends Proxy<AppPresenter> {}
    public interface MyView extends TabView, HasUiHandlers<AppUiHandlers> {}
    @Override
    protected void onBind() {
        super.onBind();
        addRegisteredHandler(IndicatorStartEvent.getType(), this);
    }

    public void onAsyncCallFail(AsyncCallFailEvent event) {
    // fireEvent is executed from: com.gwtplatform.mvp.client;PresenterWidget
        fireEvent(new IndicatorStartEvent("Firing message"));
    }

    @Override
    public void onIndicatorProgressStart(IndicatorStartEvent event) {
        MaterialToast.fireToast("Indicator start: " + event.getMessage());
    }
}

如果我从f.e。:AppPresenter(上面的代码)或GwtRESTY过滤器/回调屁股中触发此事件,请执行以下操作:

class ProgressIndicatorFilter implements DispatcherFilter {
    private AssistedInjectionFactory factory;
    private EventBus eventBus;

    @Inject
    public ProgressIndicatorFilter(AssistedInjectionFactory factory, EventBus eventBus) {
        this.factory = factory;
        this.eventBus = eventBus;
    }
    @Override
    public boolean filter(Method method, RequestBuilder builder) {
        builder.setCallback(factory.createProgressIndicatorCallback(method));
        eventBus.fireEvent(new IndicatorStartEvent("Rest-Gwt Comunication started"));
        return true;
    }
}

按预期工作。但在这些工作示例中,它使用com.google.web.bindery.event.shared;EventBus 触发事件不能使用widget,其中使用了: com.google.gwt.event.shared;HandlerManager;Bus课程。此课程Bus扩展了com.google.web.bindery.event.shared.SimpleEventBus,从EventBus扩展了正确的com.google.web.bindery.event.shared;EventBus课程。

因此小部件的方法fireEvent()使用其他EventBus

任何人都可以帮我吗?

我是红官和这条指令: http://blog.arcbees.com/2015/04/01/gwt-platform-event-best-practices-revisited/但到目前为止没有运气。请帮忙。

1 个答案:

答案 0 :(得分:2)

它不起作用,因为您的FileUploadWidget使用了自己的EventBus,而不是GWTP也使用了Presenters

有两种解决方案:

  1. 请勿使用fireEvent(new IndicatorStartEvent("Message")),而是在eventBus.fireEvent(new IndicatorStartEvent("Message"))内注入的EventBus上使用Widget

  2. 直接将IndicatorStartEvent处理程序添加到FileUploadWidget,而不是使用addRegisteredHandler上的Presenter

  3. 我更喜欢解决方案2:

    public class FileUploadWidget extends Composite {
        MaterialFileUploader uploader = new MaterialFileUploader();
    
        @Inject
        public FileUploadWidget(String triggerId) {
            super();
            initWidget(uploader);
            Window.alert("TEST Start");
            fireEvent(new IndicatorStartEvent("Message"));
        }
    }
    

    在使用Presenter的{​​{1}}或准确View中,您可以直接向FileUploadWidget添加处理程序:

    FileUploadWidget