Thanks for the reply! I am trying to digest this as I am new to GWT
and Javascript so please bare with me.
Ok, so clearly I am missing something here. Currently I am not
calling static methods (is this required?) for the callback because I
want to be able to access member variables. I have the following
public interface EventHandler {
void onEvent(String channel, String message);
}
native void nativeMethod(String channel, EventHandler handler) /*-{
$wnd.subscribe(channel,
[email protected]::onEvent
(Ljava/lang/String;Ljava/lang/String;));
}-*/;
but when the onEvent gets called I can only access some final
variables (none of the GXT members can be accessed, really only public
strings). In order to make this work I have to make my member
variables static, otherwise everything comes up as undefined, which is
why I believe this is a scope issue. Should what I am doing work?
I am also trying to do the following (which I think is your
suggestion)
native void nativeSubscribe(String channel, EventHandler handler) /*-{
var that = handler;
$wnd.subscribe(function(channel, handler) {
[email protected]::onEvent(Ljava/lang/String;Ljava/
lang/String;)(that, channel, message);
});
}-*/;
but that did not work at all. Any thoughts?
On Sep 15, 10:23 am, Thomas Broyer <[email protected]> wrote:
> 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) {
> �[email protected]::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
-~----------~----~----~----~------~----~------~--~---