Tuesday, August 15, 2017

Re: Accessing "basic" JS Object in JsInterop callback

package xxx;

import jsinterop.annotations.JsFunction;

public interface Callback {
void call(Object event);

If the JS library does not pass totally different JS objects into the callback but instead something that is worth defining as a class you can use void call(BroadCastEvent event) instead of using Object and define BroadCastEvent (or whatever name you choose) using the approach shown by Vassilis.

If the JS library passes in different kinds of JS objects then there needs to be some sort of discriminator property so you can check in Java which kind of Js object you have received, e.g. { messageType: 'BROADCAST' }. Then you could make this property available through JsInterop and use it to check the event type and hard cast it into one of the native JsTypes you have defined (one per messageType value). I think there is also a util method for casting in jsinterop.base library.

If the callback input is totally random, then it might be better to define a native JsType with just a getProperty(String name) JsOverlay method that delegates to a JSNI util method or an appropriate method in elemental.base library. In that case it is more a map like object.

In any case if you feel like you would need to use JSNI for something then first check the jsinterop.base library if it already has the util method you are looking for, because these methods will also work in J2CL if you will ever use it. If you can not find the method you need in jsinterop.base library then I think it is better to make a small utility JS library on your own and make it accessible through JsInterop instead of using JSNI.

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