Does GWT JSNI support callbacks?
Asked Answered
G

1

19

I am building a GWT app that uses Web SQL Local Storage ( http://dev.w3.org/html5/webdatabase/ ). The problem is that the Web SQL API uses callback functions as arguments.

Is it possible to pass "Java" callbacks to JSNI?

Guarantee answered 28/7, 2010 at 20:11 Comment(0)
P
22

Yes, it does:

private static native void doThingWithCallback() /*-{
  var self = this;
  var callbackFn = $entry(function(val) {
    [email protected](Ljava/lang/String;)(val);
  });
  $wnd.someApiThatTakesACallback(callbackFn);
}-*/;

Two things to remember:

  1. $entry() reminds GWT to keep track of the code when using the debugger.
  2. var self = this keeps the reference to this inside the function -- otherwise this will be the function itself...
Photodrama answered 28/7, 2010 at 20:23 Comment(3)
You are supposed to have :: before the method name: [email protected]::aMethod(Ljava/lang/String;)(val);Berghoff
is this a javascript method or a java method? javascript doesn't have 'private static' and java doesn't have 'var'.Ovariotomy
@ClickUpvote it's a GWT native method, which let's you write native JS in you GWT Java code.Photodrama

© 2022 - 2024 — McMap. All rights reserved.