JsInterop - How to access window object

84 views
Skip to first unread message

grebesche

unread,
Nov 29, 2017, 10:44:22 AM11/29/17
to GWT Users
Hi,
I want to use JsInterop to interact with a library that define a global object in the window (`window.gantt`).
I have defined the @JsType interface for this object `gantt`.

But now, how can I access this object in my GWT code?

Thanks

Thomas Broyer

unread,
Nov 29, 2017, 11:22:45 AM11/29/17
to GWT Users
Assuming you're talking about a "singleton" (global instance), then the simplest would be to have that method somewhere (anywhere):

@JsProperty(namespace=JsPackage.GLOBAL, name="gantt")
static native Gantt getGantt();


Gantt here would be your native JsType (beware of the namespace="" and name="" on the JsType if you don't want class cast exceptions at runtime; if the type is unknown, then use a Java interface to model it, or use namespace=JsPackage.GLOBAL,name="Object" to make sure that a JS "instanceof" would work)

You could also declare a native JsType representing the global scope (window) with a field to get that "gantt" object, similarly to how Elemental 2 works:
@JsType(isNative=true, name="window", namespace=JsPackage.GLOBAL)
class GanttGlobal {
 
public static Gantt gantt;
}


Guillaume REBESCHE

unread,
Nov 29, 2017, 11:37:54 AM11/29/17
to google-we...@googlegroups.com
Yes that's exactly what I needed.
Thanks.


Guillaume

--
You received this message because you are subscribed to a topic in the Google Groups "GWT Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-web-toolkit/s2eoPaFoWyI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-web-toolkit+unsub...@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages