Monday, June 17, 2019

Re: JSNI - access inner class attribute from external JS?


Thanks for your help Jens.  Is there some documentation that explains this syntax to any degree?  All I can find is this and it isn't enough - http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html#calling

Thats the only information on the official site. http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html#methods-fields contains a short description how a JSNI method call is constructed.

 
I've tried what you describe but when I call it from my external javascript I get an error indicating that the function I'm calling is not defined. 

Be aware that GWT apps load asynchronous and you have to wait for it so that $wnd.setValueFromExternal is available once your external JS executes.

 
It passes the GWT compiler but at runtime it can't find the function.  I feel like the syntax isn't correct but I'm not seeing it.  Should "Bar" be the instance name of the internal class within the outer class?  I tried that too, same result.

So it sounds like you want setValue() to be an instance method. But at the same time it sounds like your external JS does not know anything about the Bar instance on which the external JS needs to call setValue(). In that case I would probably do


class Foo {

 
class Bar {

   
static void setValueFromExternal(int value) {
     
Bar barInstance = getBarInstanceSomeHow();
      barInstance
.setValue(value);
   
}

 
}
}

public static native void exportSetValueFromExternalFunction() /*-{
  // Note: when exporting functions do not write "setValueFromExternal(I)()" because that will call the function
  $wnd.setValueFromExternal = $entry(@com.example.Foo.Bar::setValueFromExternal(I));
}-*/


The above assumes that it is more natural to use Java code to get the Bar instance somehow. Of course you could also do it within the JSNI method but I would try to avoid real code in JSNI methods because that makes it easier to switch to JsInterop (something you should consider if you want that GWT code to be GWT 3 compatible).


-- J.

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit/0ffe40da-adf8-44cb-9207-c2cb7fc26715%40googlegroups.com.

No comments:

Post a Comment