如何使用GWT EventBus [英] How to use the GWT EventBus

查看:140
本文介绍了如何使用GWT EventBus的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉GWT,想知道如何使用EventBus,或者如果有更好的解决方案通过项目发送事件。



小部件1有一个按钮。小部件2有一个标签,当我按下按钮应该改变。
这些小部件在DockLayout中:

  RootLayoutPanel rootLayoutPanel = RootLayoutPanel.get(); 
DockLayoutPanel dock = new DockLayoutPanel(Unit.EM);

dock.addWest(new Widget1(),10);
dock.add(new Widget2());

rootLayoutPanel.add(dock);

我在Widget 1中宣布了一个handleClickAlert




void handleClickAlert(ClickEvent e){
// fireEvent(e);
}

希望有人可以帮助我。谢谢!

解决方案

当将项目划分为逻辑部分(例如使用MVP)时,有时需要进行沟通。典型的通信是发送状态变化,例如:




  • 用户登录/已出去。

  • 用户直接通过URL导航到页面,因此菜单需要更新。



在这些情况下,使用事件总线是非常合乎逻辑的。 p>

要使用它,您将实例化每个应用程序的一个EventBus,然后由所有其他类使用。为了实现这一点,使用静态字段,工厂或依赖注入(在GWT的情况下为GIN)。



您自己的事件类型的示例:

  public class AppUtils {

public static EventBus EVENT_BUS = GWT.create(SimpleEventBus.class);
}

通常你也可以创建自己的事件类型和处理程序:

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

和处理程序:

  public interface AuthenticationEventHandler扩展EventHandler {
void onAuthenticationChanged(AuthenticationEvent authenticationEvent);
}

然后你这样使用:

  AppUtils.EVENT_BUS.addHandler(AuthenticationEvent.TYPE,new AuthenticationEventHandler(){
@Override
public void onAuthenticationChanged(AuthenticationEvent authenticationEvent){
//认证变更 - 做某事
}
});

并触发事件:

  AppUtils.EVENT_BUS.fireEvent(new AuthenticationEvent()); 


I am not yet familiar with GWT and wonder myself how to use the EventBus or if there are some better solutions to send an Event through the project.

Widget 1 has a Button. Widget 2 has a Label, that should change when I press the button. These widgets are in a DockLayout:

    RootLayoutPanel rootLayoutPanel = RootLayoutPanel.get();
    DockLayoutPanel dock = new DockLayoutPanel(Unit.EM);

    dock.addWest(new Widget1(), 10);
    dock.add(new Widget2());

    rootLayoutPanel.add(dock);

I have declared an handleClickAlert in the Widget 1

@UiHandler("button")
void handleClickAlert(ClickEvent e) {
    //fireEvent(e); 
}

Hopefully somebody can help me out. Thanks!

解决方案

When you divide the project into logical parts (for example with MVP) than different parts sometimes need to communicate. Typical communication is sending status changes, e.g.:

  • user logged-in / loged-out.
  • user navigated directly via URL to page so menu needs to be updated.

Using event bus is quite logical in those cases.

To use it you instantiate one EventBus per app which is then used by all other classes. To achieve this 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());

这篇关于如何使用GWT EventBus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆