Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does GWT JSNI support callbacks?

Tags:

gwt

jsni

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?

like image 845
Julio Faerman Avatar asked Jul 28 '10 20:07

Julio Faerman


1 Answers

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...
like image 127
Jason Hall Avatar answered Sep 26 '22 01:09

Jason Hall