Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Google jsapi in a GWT app

Tags:

java

gwt

jsapi

jsni

I'm trying to use Google's javascript API in my GWT application (to use google visualizations) and I'm having trouble referencing the google object in my JSNI. I'm getting a javascript error: "google is not defined".

I'm aware there's a GWT wrapper API for this but it doesn't have the functionality I need. I followed all the suggestions here: use visualization api of google in GWT but I'm still getting this error.

I've added this line to my gwt.xml file:

<script src="https://www.google.com/jsapi"></script>

I'm not sure where to put it so I added it below my main tag. That other stack question said to add it to add it to my HTML, so I'm assuming this is what they meant.

Here's the stripped down native method I'm calling that's producing the "google is not defined" error:

public static native void nativeJavaScriptFunction() /*-{
    google.load('visualization', '1.0', {
        'packages' : [ 'corechart' ]
    });
}-*/;

I've also tried referencing google as "$wnd.google" and "$doc.google". I'm using the latest version of GWT 2.5.1. Does anybody else have any javascript api's working and referenced in a JSNI method?

like image 343
Zip184 Avatar asked Jul 25 '26 11:07

Zip184


1 Answers

Instead of adding that script tag in your host page, you could use ScriptInjector instead, like this:

ScriptInjector
    .fromUrl("http://api.elsevier.com/javascript/scopussearch.jsp")
    .setCallback(new Callback<Void, Exception> () {
        @Override
        public void onFailure(Exception reason) {
            throw new UnsupportedOperationException("FAILURE to inject Scopus API !!!");
        }

        @Override
        public void onSuccess(Void result) {
            System.out.println("Elsevier scopus search API successfully injected...");
        }
    }).setWindow(ScriptInjector.TOP_WINDOW).inject();

Then you should be able to successfully use the injected API via JSNI.

like image 115
Răzvan Petruescu Avatar answered Jul 28 '26 14:07

Răzvan Petruescu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!