大規模なアプリケーションを構築しており、初期化、ユーザー管理などのコア モジュール、カスタマー モジュール、プロダクション モジュールなどの複数のモジュールに分割したいと考えています。</p>
複数の GWT モジュール (GWT 分割手法を使用しない) に分割し、LoginEvent、LogoutEvent などのイベントをブロードキャストするために EventBus を共有したいと考えています。コンパイル時間を短縮し、変更したモジュールのみを再コンパイルしたいので、コード分割手法を使用したくありません。これにより、HTML ホスト ページのスクリプト タグにコメントを付けることで、モジュールを有効または無効にすることもできます。
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;
}-*/;
しかし、ブラウザで実行すると、Firebug で次の例外が発生します。
uncaugth exception [object Object]
顧客イベントを実装/インターフェースする MyEvent および MyEventHandler クラスもコピーしました。
PS: コンパイルを避けるために、他のモジュール参照にコメントを付けるテクニックも知っています。