Hi ChrisK, You need to understand how JavaScript works.
Like in many languages, the "this" keyword means the current object. The JavaScript language allows you to manipulate code as a function. This function can be affected to any variable or object member, like: $wnd.js_callback = function() { ... } Then, you can call your code like this: $wnd.js_callback() In the function body, the "this" variable is $wnd. If you assign the same function to the member of another object, the "this" variable will be equal to the new object. >From JSNI, "@mypackage.MyClass::MyMethod()" references the JavaScript function corresponding to the MyMethod method of mypackage.MyClass. This method can be static or not, the reference is the same (no need of "this" before "@..." just to obtain the JavaScript function). Calling "$entry" just add a function wrapper around it but it stay a function. When you assign this function to "$wnd.js_callback", the "this" variable inside of the function will be "$wnd". If MyMethod is static, it's okay. If MyMethod is not static, GWT will probably "cast" the $wnd to a MyClass... it's bad... The "call" method of a JavaScript function allows you to call it with a differente "this". Then, using "$wnd.js_callback.call(otherObject)", the "this" variable reference otherObject. So the glue is: var that = this; $wnd.js_callback = function() { $entry(@mypackage.MyClass::MyMethod()).call(that); } When "js_callback" will be called, MyMethod is called on "that", the JavaScript object representing the MyClass instance. Finally, to avoid memory leaks of closure: $wnd.js_callback = (function(obj) { return function() { $entry(@mypackage.MyClass::MyMethod()).call(obj); }; })(this); And remember, for static methods it's just: $wnd.js_callback = $entry(...); Good luck, Olivier On 22 mar, 20:16, ChrisK <cknow...@gmail.com> wrote: > Thanks for the reply! > > I'm not sure I understand why "this" evaluates to the plain JS > context. I was going off the documentation here which seems to suggest > you can use "this" in this > way:http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI... > > Or perhaps you just can't use it for $entry type JSNI? > > Having said that, I've updated to a non-static method - silly me. I > did actually try that first but only pasted in my last attempt at > getting some working code. I have tried both of the below now with the > same stack trace as a result. Note, there are not actually any spaces > before the .@ in my code I'm just working around Google Groups > mangling it. > > private native void initialiseCallbacks() /*-{ > var that = this; > $wnd.js_callback = > $entry(that ....@com.mine.playerimpl::callback()); > > }-*/; > > and > > private native void initialiseCallbacks(PlayerImpl pl) /*-{ > $wnd.js_callback = $entry(pl ....@com.mine.playerimpl::callback()); > > }-*/; > > On Mar 22, 5:17 pm, Thomas Broyer <t.bro...@gmail.com> wrote: > > > On Mar 22, 4:23 pm, ChrisK <cknow...@gmail.com> wrote: > > > > My above code got a little mangled along the way, the $entry part has > > > "(this", followed by a full stop, followed by > > > "@com.mine.PlayerImpl::callback());" > > > And that's the problem: "this" is a special keyword and will evaluate > > to the "this" context of the calling "plain JS" code, whereas you'd > > want it to be some PlayerImpl instance. > > > There are several ways to fix this: > > // evaluates "this" right now > > var that = this; > > // then uses "that" > > $wnd.js_callback = $entry(t...@com.mine.playerimpl::callback()); > > > or pass the instance as an argument to the method and then use its > > name. > > > But there's another bug in your code: there's no "this" in a "static" > > method! -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.