开发者

Does GWT JSNI support callbacks?

开发者 https://www.devze.com 2023-01-09 01:43 出处:网络
I am building a GWT app 开发者_Python百科that uses Web SQL Local Storage ( http://dev.w3.org/html5/webdatabase/ ).

I am building a GWT app 开发者_Python百科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?


Yes, it does:

private static native void doThingWithCallback() /*-{
  var self = this;
  var callbackFn = $entry(function(val) {
    self.@com.your.package.AClass.aMethod(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...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号