jsinterop

531 views
Skip to first unread message

Eric Nissan

unread,
Mar 1, 2016, 3:03:03 PM3/1/16
to GWT Users
sorry if this has been answered, I just started using jsinterop in gwt 2.8.  What is the recommended way populate data into a jsinterop javascript object from my server.  Assuming I have Json, do we still use OverlayTypes?  or is there a better way to do it?

Always appreciative,
Eric

Ümit Seren

unread,
Mar 2, 2016, 9:36:43 AM3/2/16
to GWT Users

This works:

{'somecollection':['String1','String2'],'somestring':'text'}
@JsType(isNative = true,namespace = JsPackage.GLOBAL,name="Object")
public interface Data {
    @JsProperty
    String[] getSomecollection();
    @JsProperty
    String getSomestring();
}

Data data = JsonUtils.safeEval(json).cast();

Eric Nissan

unread,
Mar 3, 2016, 10:34:47 PM3/3/16
to GWT Users
Thanks a ton!  that worked.
followup, how do I set a List<Data>?
I tried to use JsArray but that requires a JavaScriptObject, which Data is not.

Thanks,
Eric

Ümit Seren

unread,
Mar 4, 2016, 2:02:34 AM3/4/16
to google-we...@googlegroups.com

I think you you have to use a normal array: Data[]


--
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/nmBAOX4vcSo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-web-tool...@googlegroups.com.
To post to this group, send email to google-we...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

Eric Nissan

unread,
Mar 4, 2016, 8:18:17 AM3/4/16
to GWT Users
I tried that also, but

Data[] thing = JsonUtils.safeEval(response.getText()).cast();

does not compile as Data[] is not a JavaScriptObject

To unsubscribe from this group and all its topics, send an email to google-web-toolkit+unsub...@googlegroups.com.

Ümit Seren

unread,
Mar 4, 2016, 9:36:44 AM3/4/16
to google-we...@googlegroups.com
AFAIK you can't serialize an array directly. You have to put it into a wrapper object. 

To unsubscribe from this group and all its topics, send an email to google-web-tool...@googlegroups.com.
To post to this group, send email to google-we...@googlegroups.com.

--
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/nmBAOX4vcSo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-web-tool...@googlegroups.com.

Eric Nissan

unread,
Mar 4, 2016, 4:46:48 PM3/4/16
to GWT Users
I don't know if this is the best way, but I got it to work by making my own implementation of  jsArray.

I changed the generic definition from 

public class JsArray<T extends JavaScriptObject> extends JavaScriptObject {


to 

public class JsArray<T> extends JavaScriptObject


which worked with my jsinterop interface.

the problem I see is that although the documentation says an interface market JsType will be treated as a JavaScriptObject when casting, that does not help for using it in place of a generic.

I don't know what will blow up from this, but it seems to work for me.  If there is a better way to do this, I am all ears :)

Thanks,
Eric
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-we...@googlegroups.com.

--
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/nmBAOX4vcSo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-web-toolkit+unsub...@googlegroups.com.

Goktug Gokdogan

unread,
Mar 16, 2016, 11:26:07 AM3/16/16
to google-web-toolkit
On Fri, Mar 4, 2016 at 5:18 AM, Eric Nissan <eric....@gmail.com> wrote:
I tried that also, but

Data[] thing = JsonUtils.safeEval(response.getText()).cast();

does not compile as Data[] is not a JavaScriptObject


Data[] thing =  (Data[])(Object) JsonUtils.safeEval(response.getText());

should work. Also if you choose do define your own Json.parse accessor, you won't need the cast:

@JsMethod(namespace="JSON")
private static native <T> T parse(String text);
 
To unsubscribe from this group and all its topics, send an email to google-web-tool...@googlegroups.com.
To post to this group, send email to google-we...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

--
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-tool...@googlegroups.com.

Eric Nissan

unread,
Mar 25, 2016, 10:48:28 AM3/25/16
to GWT Users
Thank You.  I forgot about that doc you had written.  Helped me tons!
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-we...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

--
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+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages