-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for multiple window contexts for globals #4
Comments
I don't test the code but I think the code below could work: import elemental2.core.JsDate;
import elemental2.base.JsConstructorFn;
@JsType(isNative = true)
interface HasDateCtor {
@JsProperty(name = "Date")
public JsConstructorFn<JsDate> getDateCtor();
}
//...
JsDate a = ((HasDateCtor)iframe.contentWindow).getDateCtor().construct();
JsDate b = new JsDate(); you could even add default method newDate() on the interface and invoke it directly: @JsOverlay
default JsDate newDate() {
return getDateCtor().construct();
}
JsDate a = ((HasDateCtor)iframe.contentWindow).newDate(); Like I said I didn't test the code but this is what came first to my mind. |
Possibly. It's all dependent on the output of J2CL, and I don't have access to that. In any case, the proposed workaround starts getting messy when working with namespaced system classes like Intl.Collator or something. I was thinking of having an explicit method in jsinterop.base.Js like
that would magically work when used like this:
And maybe there could also be a version that explicitly invokes the constructor as well
Though I'm not sure if J2CL changes would be needed to get something like that to work. With a Js method like that, you could use an iframe to load up some code and define some classes, like GWT. And you could reach in there and create objects from those classes without needing to create a whole bunch of separate factory classes. |
Oh, it would also be good if this worked with JS Interop interfaces that represent JS classes too. I'm not sure if J2CL already does this or not.
|
This is sort of obscure, but it would be nice to have support for the same classes existing in different global window contexts. For example, if you have an iframe, it will have its own set of global system classes in its own window object. Sometimes, you need to construct objects from this separate context or do weird comparisons etc.
Whatever pattern you come up with for that might also be useful when running JavaScript code inside Java because in that case, you might have multiple JavaScript contexts, and you need to specify which JS context you are constructing objects in.
The text was updated successfully, but these errors were encountered: