On 15 sep, 04:24, JaM <[email protected]> wrote:
> I previously posted about Shindig, when probably more appropriately I
> should have posted this question (sorry for the dup).
>
> I have successfully implemented a callback in Java (using an
> interface) which is called from Javascript appropriately, but after
> doing so it seems something is not quite right (perhaps the scope, not
> really sure how to determine).  The issue is that I now get an error
> that says something like jV(this.a.a...) and it throws an error saying
> this.a is undefined.  The issue only happens when I am trying to
> access instance variables inside my java code.  If I do not access
> instance variables everything works fine, which leads me to believe it
> may be a scoping issue.  Is there a general rule of thumb for how to
> do this correctly in GWT?  Any pointers would be greatly appreciated.

This generally works:

public interface MyCallback {
   void callback(int a, int b, String c);
}

public native void doSomething(MyCallback callback) /*-{
    $wnd.doSomething(function(a, b, c) {
        [email protected]::callback(IILjava/lang/String;)
(a, b, c);
    });
}-*/;

Actually, you'd generally call some static method that uses the
UncaughtExceptionHandler if there's one:
public native void doSomething(MyCallback callback) /*-{
    $wnd.doSomething(function(a, b, c) {
        @my.package.MyClass::callback(Lmy/package/MyCallback;IILjava/
lang/String;)(a, b, c, callback);
    });
}-*/;
private static void callback(MyCallback callback, int a, int b, String
c) {
   UncaughtExceptionHandler handler = GWT.getUncaughtExceptionHandler
();
   if (handler != null) {
      callbackImpl(callback, a, b, c);
   } else {
      callbackAndCatch(handler, callback, a, b, c);
   }
}
private static void callbackImpl(MyCallback callback, int a, int b,
String c) {
   callback.callback(a, b, c);
}
private static void callbackAndCatch(UncaughtExceptionHandler handler,
MyCallback callback, int a, int b, String c) {
   try {
      callbackImpl(callback, a, b, c);
   } catch (Throwable t) {
      handler.onUncaughtException(t);
   }
}

..and finally, if your callback is "this":
public native void doSomething() /*-{
   var that = this;
   $wnd.doSomething(function(a, b, c) {
     @my.package.MyClass::callback(Lmy/package/MyCallback;IILjava/lang/
String;)(that, a, b, c);
   });
}-*/;


You'll find those patterns several times in GWT itself, as well as in
other GWT libraries wrapping JavaScript libs (GALGWT, GWT-in-the-AIR,
etc.)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to