Combining Javascript into a GWT Application

I am writing an application in GWT ( mapfaire.com ) that uses the Google Maps API, and I would like to use the library utility . Writing a JSNI wrapper for libraries is not a problem, but how can I ensure that the compiler "bakes" JS in the application itself, rather than loading them separately through the script module ?

+5
source share
4 answers

ScriptInjector http://gwt-code-reviews.appspot.com/1451818/ added to gwt 2.4

   ScriptInjector.fromUrl("http://example.com/foo.js").setCallback(
     new Callback<Void, Exception>() {
        public void onFailure(Exception reason) {
          Window.alert("Script load failed.");
        }
        public void onSuccess(Void result) {
          Window.alert("Script load success.");
        }
     }).inject();
+6
source

You can define a ClientBundle for your JavaScript resources, like this:

public interface JsResources extends ClientBundle {
    final JsResources INSTANCE = GWT.create(JsResources.class);

    @Source("myAwesomeJavaScript.js")
    TextResource myAwesomeJavaScript();
}

and include it in your application using ScriptInjector:

ScriptInjector
    .fromString( JsResources.INSTANCE.myAwesomeJavaScript().getText() )
    .inject();
+8

, JS- JSNI.

TextResource :

public static interface Resources extends ClientBundle {
    @Source("some.js")
    TextResource someJs();
}

public void onModuleLoad() {
    final Resources resources = GWT.create(Resources.class);
    eval(resources.someJs().getText());
}

private static native void eval(String s) /*-{
    eval(s);
}-*/;

The text "some.js" will be placed directly in the resulting application. There will be no separate download.

+2
source

Is the gwt-maps-utility project useful for your needs?

0
source

All Articles