Wednesday, November 29, 2017

Re: JsInterop - How to access window object


On Wednesday, November 29, 2017 at 4:44:22 PM UTC+1, grebesche wrote:
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?

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


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

No comments:

Post a Comment