0

My GWT app is designed to have an iframe with another web app (not GWT this time) embedded in it. This embedded app expects to be able to call some APIs in my parent app like so:

window.parent.f(function(result) { console.log("Result == " + result); });

How do I declare a JSNI function that can take that function as a parameter?

private static final void f(Consumer<String> onsuccess) {
    onsuccess.accept("abcd");
}

public static native void installApi() /*-{
    $wnd.f = function(onsuccess) {
        @my.package.client.Example::f(...); // What goes in here?
    }
}-*/;
Stik
  • 519
  • 4
  • 17

1 Answers1

1

Look at JsInterop. It's currently best and easiest approach to communicate between Java and JavaScript in GWT.

http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJsInterop.html

Beside that, there are security limitations when Iframe is loaded from different domain / port.

Calling a parent window function from an iframe

EDIT: Try this:

@JsType(name = "Foo", namespace = JsPackage.GLOBAL)
public class Foo {
    @JsFunction
    public interface Callback {
        public abstract void onCallback(String msg);
    }

    public void test(Callback callback) {
        GWT.log("Message from Java");
        callback.onCallback("Massage passed from Java");
    }
}

And call this by:

var foo = new $wnd.Foo();
foo.test(function(msg) {
    console.log("Message from JavaScript");
    console.log("Received: " + msg);
});

And most important that it's hidden in docs, you need to add this option to compile and superdev mode params.

-generateJsInteropExports
pH4Lk0n
  • 21
  • 4
  • I've been reading the jsinterop docs, but i've not managed to get to grips with it yet. The security limitations are well understood, in this case the child app is loaded from the same domain – Stik Jul 02 '19 at 10:41