Does GWT JSNI support callbacks?

安稳与你 提交于 2019-12-05 10:07:23

问题


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?


回答1:


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...


来源:https://stackoverflow.com/questions/3357076/does-gwt-jsni-support-callbacks

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!