GWT:如何在两个模块之间共享一个java对象(即:EventBus) [英] GWT: How to share a java Object (ie: EventBus) between two modules

查看:144
本文介绍了GWT:如何在两个模块之间共享一个java对象(即:EventBus)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个大型应用程序,我想将它分成几个模块,如用于初始化,用户管理等的核心模块,客户模块,生产模块等等。

我想将它拆分成多个GWT模块(不使用GWT分割技术),并共享一个EventBus来广播诸如LoginEvent,LogoutEvent等事件。我不想使用代码分割技术,因为我想缩短编译时间并仅重新编译我修改的模块。
这也允许通过在HTML主页中注释脚本标签来启用或禁用模块。



我使用JSNI编写了以下代码:

  CoreModule的入口点:$ b​​ 
$ b private static SimpleEventBus eventBus = null;

public void onModuleLoad(){
export();
getEventBus()。addHandler(MyEvent.TYPE,new MyEventHandler(){
$ b @Override
public void onEvent(MyEvent myEvent){
Window.alert(myEvent .getMessage());
}
});


public static SimpleEventBus getEventBus(){
if(eventBus == null)
eventBus = new SimpleEventBus();
返回eventBus;


public static native void export()/ * - {
$ wnd.getEventBus = $ entry(@ testExporter.client.TestExporter :: getEventBus());
} - * /;

CustomerModule的EntryPoint:

public void onModuleLoad(){
Button button = new Button(Click me);
button.addClickHandler(new ClickHandler(){
$ b @Override
public void onClick(ClickEvent event){
getEventBus()。fireEvent(new MyEvent(Button点击!));
}
});
RootPanel.get()。add(button);


public static native SimpleEventBus getEventBus()/ * - {
//创建一个无用的eventBus,因为GWT编译器调用一个空实例
var eventBus = @ com.google.gwt.event.shared.SimpleEventBus :: new()();
eventBus = $ wnd.getEventBus();
返回eventBus;
} - * /;

但在Firebug中,当我在浏览器中执行时,出现以下异常:

  uncaugth异常[object Object] 

我也复制了实现/接口客户事件的MyEvent和MyEventHandler类。

PS:我也知道这种技术是为了避免编译而引用其他模块引用的注释。

解决方案

你不能在不同的GWT编译模块之间共享代码,除非通过jsni使代码的某些部分可用并通过jsni调用这些导出的方法,就像在查询中一样。但请注意:首先,共享类将不兼容,因为每个编译都会以不同的方式重命名这些类/方法,其次,每个编译都会删除不同的死代码片断。

所以在你的情况下, window.getEventBus SimpleEventBus $ c> export方法在其他模块中是未知的,尽管其他模块也使用 SimpleEventBus 以及

最简单的方法就是使用 GWT导出器。首先,在每个模块中正确选择要导出的js-api,如何命名它,并实现 Exportable 并方便地注释方法。其次考虑你将用于沟通的对象,因为其中一些对象可能不兼容。我会使用GWT-exporter支持的原始类型,JavaScript对象和函数



我认为,对于共享类,如果您使用GWT导出器对其进行注释相同的命名空间和你导出相同的方法,希望你可以在所有模块中使用,但我不确定。

因此,通过jsni或gwt-exporter导出一个js API并在它们之间传输纯原始或js对象。


I’m building a large application and I would like to split it in several modules like Core Module for initialization, users management, etc…, Customer Module, Production Module, etc…

I want to split it in multiples GWT modules (not using GWT splitting technique) and share an EventBus for broadcast some events like LoginEvent, LogoutEvent. I don’t want uses the code splitting technique because I want reduce the compile time and re-compile only the module that I modified. This allow also to enable or disable a module by commenting the script tag in the HTML host page.

I’ve write the following code with using JSNI:

CoreModule’s EntryPoint:

private static SimpleEventBus eventBus = null;

public void onModuleLoad() {
    export();
    getEventBus().addHandler(MyEvent.TYPE, new MyEventHandler() {

        @Override
        public void onEvent(MyEvent myEvent) {
            Window.alert(myEvent.getMessage());
        }
    });
}

public static SimpleEventBus getEventBus() {
    if (eventBus == null)
        eventBus = new SimpleEventBus();
    return eventBus;
}

public static native void export() /*-{
    $wnd.getEventBus = $entry(@testExporter.client.TestExporter::getEventBus());
}-*/;

CustomerModule’s EntryPoint:

public void onModuleLoad() {
    Button button = new Button("Click me");
    button.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            getEventBus().fireEvent(new MyEvent("Button clicked !"));
        }
    });
    RootPanel.get().add(button);
}

public static native SimpleEventBus getEventBus() /*-{
    // Create a useless eventBus because the GWT compiler make a call to a null instance
    var eventBus = @com.google.gwt.event.shared.SimpleEventBus::new()();
    eventBus = $wnd.getEventBus();
    return eventBus;
}-*/;

But I’ve the following exception in Firebug when executing in the browser:

uncaugth exception [object Object]

I copied also the MyEvent and MyEventHandler classes that implements/interfaces a customer event.

P.S.: I know also the technique that consist to comment the other modules references to avoid to compile it.

解决方案

You cannot share code between different GWT compiled modules, unless you make some parts of your code available via jsni and call these exported methods via jsni, like you are trying in your query.

But be aware that: first, shared classes would be incompatible because each compilation would rename the classes/methods in a different way, and second, each compilation would remove different dead code pieces.

So in your case the SimpleEventBus returned in your window.getEventBus exported method is not known in other modules, although the other modules are using SimpleEventBus as well

The easiest way to do what you want, is to use GWT-exporter. First select correctly the js-api you want to export in each module, how you want to name it, and implement Exportable and annotate methods conveniently. Second take in account which objects would you use for the communication, because some of then could be incompatible. I would use primitive types, javascript object, and functions which are supported in GWT-exporter

I think that with GWT-exporter, for shared classes, if you annotate them in the same namespace and you export the same methods, hopefully you could use then in all modules but I'm not sure.

So export a js API via jsni or gwt-exporter and transfer pure primitive or js objects between them.

这篇关于GWT:如何在两个模块之间共享一个java对象(即:EventBus)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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