我建立一個大的應用程序,我想它在幾個模塊,比如核心模塊進行初始化,用戶管理等之間的分裂(即EventBus)......,客戶模塊,生產模塊等...GWT:如何分享一個Java對象:兩個模塊
我想分割成多個GWT模塊(不使用GWT分割技術),並共享一個EventBus廣播一些事件,如LoginEvent,LogoutEvent。我不想使用代碼分割技術,因爲我想縮短編譯時間並僅重新編譯我修改的模塊。 這也允許通過在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類。
P.S:我也知道,包括評論中的其它模塊的引用,以避免編譯它的技術。