On 9 mar, 23:13, Eric <[email protected]> wrote:
> Host page has this:
>
> Call JSNI JavaScript function from HTML where function implementation
> was written in Java. Click here: <a href='#' onClick='gwtJs2();'>here</
> a>
>
> In EntryPoint, I have this:
>         public void onModuleLoad() {
>                 defineGwtJs2();
>         }
>         /**
>          * Define JS function in GWT that will be implemented by GWT Java
>          */
>         public native void defineGwtJs2() /*-{
>                 $wnd.gwtJs2 =
> [email protected]::callGwtJs2Impleme
>  ntation();
>         }-*/;
>
>         public void callGwtJs2Implementation(){
>                 Window.alert("Here was clicked");
>         }
>
> When I start in hosted mode and click the link, I see the following in
> the FireBug console:
> uncaught exception: java.lang.ClassCastException
>
> If I compile and move the html/js to tomcat, the alert pops up as
> expected.  Is there a known issue around this?  Or am I doing
> something that's not supported?

This is a scoping issue in JS, in that the "this" context is lost when
you assign the function/method. This is just how JS works. The proper
way is to introduce a "closure":

var that = this;
$wnd.gwtJs2 = function()
{ 
[email protected]::callGwtJs2Impleme
ntation()(); }

(make sure ou call the method on the "that" variable, not on the
"this" special keyword)

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