Mar*_*ark 48 java events gwt event-handling
我想知道如何使用EventBus或者是否有更好的解决方案来Event通过项目发送.
Widget1有一个Button.Widget2有一个Label,当我按下按钮时应该改变.这些小部件位于DockLayout:
RootLayoutPanel rootLayoutPanel = RootLayoutPanel.get();
DockLayoutPanel dock = new DockLayoutPanel(Unit.EM);
dock.addWest(new Widget1(), 10);
dock.add(new Widget2());
rootLayoutPanel.add(dock);
Run Code Online (Sandbox Code Playgroud)
我已宣布handleClickAlert进入Widget1:
@UiHandler("button")
void handleClickAlert(ClickEvent e) {
//fireEvent(e);
}
Run Code Online (Sandbox Code Playgroud)
Pet*_*ego 105
当您将项目划分为逻辑部分(例如使用MVP)时,不同的部分有时需要进行通信.典型的此通信是通过发送状态更改来完成的,例如:
在这些情况下,使用事件总线是非常合理的.
要使用它,您需要EventBus为每个应用程序实例化一个,然后由所有其他类使用.为此,请使用静态字段,工厂或依赖注入(在GWT的情况下为GIN).
您自己的事件类型的示例:
public class AppUtils{
public static EventBus EVENT_BUS = GWT.create(SimpleEventBus.class);
}
Run Code Online (Sandbox Code Playgroud)
通常,您还可以创建自己的事件类型和处理程序:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
和处理程序:
public interface AuthenticationEventHandler extends EventHandler {
void onAuthenticationChanged(AuthenticationEvent authenticationEvent);
}
Run Code Online (Sandbox Code Playgroud)
然后你像这样使用它:
AppUtils.EVENT_BUS.addHandler(AuthenticationEvent.TYPE, new AuthenticationEventHandler() {
@Override
public void onAuthenticationChanged(AuthenticationEvent authenticationEvent) {
// authentication changed - do something
}
});
Run Code Online (Sandbox Code Playgroud)
并解雇事件:
AppUtils.EVENT_BUS.fireEvent(new AuthenticationEvent());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37336 次 |
| 最近记录: |