Tested and it works.
Source : http://stackoverflow.com/questions/6030202/how-to-use-the-gwt-eventbus
When you divide the project into logical parts
(for example with MVP) than several different parts sometime need to
communicate. Typical communication is sending some status changes, e.g.:
To use it you instantiate one EventBus per app which is then used by all other classes. To achieve this you use static field, factory or dependency injection (GIN in case of GWT).
Example with you own event types:
and the handler:
Then you use it like this:
and fire the event:
- user logged-in / loged-out.
- user navigated directly via URL to page so menu needs to be updated.
To use it you instantiate one EventBus per app which is then used by all other classes. To achieve this you use static field, factory or dependency injection (GIN in case of GWT).
Example with you own event types:
public class AppUtils{
public static EventBus EVENT_BUS = GWT.create(SimpleEventBus.class);
}
Normally you'd also create your own event types and handlers:
public class AuthenticationEvent extends GwtEvent<AuthenticationEventHandler> {
public static Type<AuthenticationEventHandler> TYPE = new Type<AuthenticationEventHandler>();
@Override
public Type<AuthenticationEventHandler> getAssociatedType() {
return TYPE;
}
@Override
protected void dispatch(AuthenticationEventHandler handler) {
handler.onAuthenticationChanged(this);
}
}
and the handler:
public interface AuthenticationEventHandler extends EventHandler {
void onAuthenticationChanged(AuthenticationEvent authenticationEvent);
}
Then you use it like this:
AppUtils.EVENT_BUS.addHandler(AuthenticationEvent.TYPE, new AuthenticationEventHandler() {
@Override
public void onAuthenticationChanged(AuthenticationEvent authenticationEvent) {
// authentication changed - do something
}
});
and fire the event:
AppUtils.EVENT_BUS.fireEvent(new AuthenticationEvent());
0 comments:
Post a Comment