The approach is very simple and easy, you use you detached GWT app like any other JS script.
Any GWT app is a JS script.
1st you need to define the interface of the detached module in terms of JsInterop: isNative = true.
You can only use Strings, numbers, arrays and isNative = true interfaces. No Java types.
@JsType(isNative = true)
public interface Interface {
@JsMethod Double computeSomething(String arg);
@JsMethod Disposable fetchData(Consumer<Data> arg);
}
2nd you implement the interface and use Window.* namespace to communicate between modules:
class Exports {
@JsProperty(namespace = JsPackage.GLOBAL)
public static native void setInterface(Interface value);
}
Exports.setInterface(new Implementation());
3rd - use the interface in a main program:
class Imports {
@JsProperty(namespace = JsPackage.GLOBAL)
public static native Interface getInterface();
}
Runnable onLoad = () -> {
Interface i = Imports.getInterface();
i2.computeSomething("test1");
};
Runnable onError = () ->{
JsGlobals.getWindow().getConsole().log("onError");
};
ModuleLoader.loadModule(
"jsExport/jsExport.nocache.js",
progressBar,
onLoad,
onError
);
Where ModuleLoader loads the script via script element:
Document document = JsGlobals.getDocument();
HTMLScriptElement scriptElement = document.createScriptElement();
scriptElement.setSrc(name);
scriptElement.setOnerror(event -> onError.run());
scriptElement.setOnload(new State()::schedule);
document.getHead().appendChild(scriptElement);
We use timer to understand if the module loading is completed, however other ways are possible:
private Runnable waitForObject = () -> {
if (isLoaded.apply()) {
onLoad.run();
} else {
schedule();
}
};
private ScheduledCommand timerCallback = () -> waitForObject.run();
private void schedule() {
JsGlobals.getWindow().setTimeout(timerCallback, 17);
}
You may have both modules in a single maven module, but different .gwt.xml files.
We unite modules for the module system testing, but not for production.
In production we have two separate maven modules.
We use single permutation for all browsers (for Chrome) and we don`t have any issues with other browsers.
<module rename-to="jsImport">
<entry-point class='jsExport.JsImportTest'/>
<source path="jsExport"/>
</module>
<module rename-to="jsExport">
<entry-point class='jsExport.JsExportTest'/>
<source path="jsExport"/>
</module>
--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment