GWT, how to fire event from widget or composite using EventBus from HandlerManager -


i have widget. fire event follow:

fireevent(new indicatorstartevent("message"));

but dosn't work.

normally use presenter (gwtp), have regular widget:

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

here event code:

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

this app presenter handle event:

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 executed from: com.gwtplatform.mvp.client;presenterwidget         fireevent(new indicatorstartevent("firing message"));     }      @override     public void onindicatorprogressstart(indicatorstartevent event) {         materialtoast.firetoast("indicator start: " + event.getmessage());     } } 

if fire event f.e.: apppresenter (code above), or gwtresty filter/callback ass follow:

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

it work expected. in working examples use com.google.web.bindery.event.shared;eventbus firing event doesnt work widget, used: com.google.gwt.event.shared;handlermanager;bus class. class bus extends com.google.web.bindery.event.shared.simpleeventbus extends proper eventbus class com.google.web.bindery.event.shared;eventbus.

so widget's method fireevent() use other eventbus.

can me this?

i've red official , instruction: http://blog.arcbees.com/2015/04/01/gwt-platform-event-best-practices-revisited/ no luck far. please help.

it not work because fileuploadwidget uses it's own eventbus , not gwtp 1 used in of presenters.

there 2 solutions:

  1. don't use fireevent(new indicatorstartevent("message")) use eventbus.fireevent(new indicatorstartevent("message")) on injected eventbus inside of widget.

  2. add indicatorstartevent handler fileuploadwidget directly instead of using addregisteredhandler on presenter.

i prefer solution 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"));     } } 

in presenter or precise view uses fileuploadwidget, add handler directly fileuploadwidget:

public class uploadview extends viewwithuihandlers<uploaduihandlers> implements uploadpresenter.myview,indicatorstartevent.indicatorhandler {      @uifield     fileuploadwidget uploadwidget;      @inject     public uploadview(final binder binder) {       widget = binder.createandbindui(this);           uploadwidget.addhandler(new indicatorstartevent.handler(),this);     }      public void onindicatorprogressstart(indicatorstartevent event) {        materialtoast.firetoast("indicator start: " + event.getmessage());     } } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -