Thursday, September 2, 2010

Re: Howto access instance method from external JavaScript?

On Sep 2, 4:11 pm, KaiWeing <KaiWe...@gmx.net> wrote:
> Hello,
>
> I am looking for a way to post to my event bus via user defined
> javascript.
>
> This is what I came up with, but the event is not fired. What's wrong?
>
> // I call this during onModuleLoad
> public native void registerExternalEventSink() /*-{
> $wnd.fireExternalEvent =
> th...@org.test.Gwt_platform_client::fireExternalEvent(Ljava/lang/
> String;);
>
> }-*/;
>
> private void fireExternalEvent(String eventData) {
> SomeEvent.fire(ginjector.getEventBus(), eventData);
>
> }
>
> js call:
>
> fireExternalEvent("mydata"); // event is not triggered
>
> Thanks for your input!

The "this" in you code will be translated in to a JavaScript "this",
which won't be the object you're expecting. You just have to avoid
using the "this" keyword as the "this object" of the method call, and
it's fortunately very easy:

public native void registerExternalEventSink() /*-{
var that = this;
$wnd.fireExternalEvent = $entry(function(str) {
that@org.test.Gwt_platform_client::fireExternalEvent(Ljava/lang/
String;)(str);
});
}-*/;

I also added the $entry() wrapper so that exceptions thrown from
within your code go through your GWT.UncaughtExceptionHandler.

--
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-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment