Because GWT code runs in an iframe, JsPackage.GLOBAL maps to the JSNI $wnd 
equivalent referencing the parent window so anything referenced from 
JsInterop needs to be recorded as a window property; and classes aren't 
recorded on the window per ECMAScript 
(see https://stackoverflow.com/a/37711826/116472), this is why you need to 
do it explicitly to expose them to JsInterop.

On Friday, February 24, 2023 at 10:41:17 PM UTC+1 [email protected] wrote:

> I am attempting to use a javascript class in GWT. Here is the sample code 
> I am using.
>
> Javascript file - rhombus.js
>
> class Rhombus {
>     static isReady() {
>         return true;
>     }
> }
>
>
> Java file - Rhombus.java 
>
> package com.xyz.graphics;
>
> import jsinterop.annotations.JsPackage;
> import jsinterop.annotations.JsType;
>
> @JsType(isNative = true, namespace = JsPackage.GLOBAL)
> public class Rhombus {
>     public static native boolean isReady();
> }
>
>
> index.html
> <script src="js/rhombus.js"></script>
> <script src="app/app.nocache.js"></script>
>
> When I try to access Rhombus.isReady(), I get 
> (TypeError) : Cannot read properties of undefined (reading 'isReady')
>
> However I can access the method correctly if I attach Rhombus to the 
> window object.
>
> <script type="text/javascript">
>     window.Rhombus = Rhombus;
> </script>
>
> I don't understand why attaching the class to the window object is 
> necessary, as none of the documentation makes a mention of that.
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-web-toolkit/ca306035-2e34-403e-aec1-8817d67a718an%40googlegroups.com.

Reply via email to