Tuesday, November 21, 2017

Re: JsInterop Shared Model + REST API

Paul,

If I go with T Broyer's suggestion of making User extend JavaScriptObject then would interop-utils be able to handle an array property User[] users within User for deserialization?
I'm trying to work around the issue of having a list deserialized as its correct type.  Ultimately I'd like to get my User[] back in browser JavaScript and be able to do
var users = ...deserialized User[]
users[0].getValue();
where getValue() is a @JsMethod

I'm realizing now that I can't do that unless @JsType(isNative = false because overlay methods can't be accessed from JavaScript.  Doing that causes a casting issue when using gwt.interop.utils.client.JSON to parse my incoming JSON string.

On Saturday, November 18, 2017 at 7:02:37 AM UTC-5, Paul Stockley wrote:
gwt-interop-utils shows one way of doing it. This assumes you are OK using intermediate collection types.




On Saturday, November 18, 2017 at 3:52:09 AM UTC-5, Chris L wrote:
I'm trying to create a shared model in my GWT 2.8.2 application that I can use both at the server and in GWT client code.

My research/Google Fu tells me that this might be possible with JsInterop but since I'm new to JsInterop I'm just not sure.
I've done some experiments but I've run into a couple of issues with the biggest being that I can't have my model accessor methods if I make the model native.  The compiler tells me that they have to be native or abstract.

What I'd like to do with my model is:
@JsType
public class User {
    private double id;
    private String code;
    private String name;

    @JsConstructor
    public User() {
    }

    @JsIgnore
    public User(double id, String code, String name) {
        this();

        this.id = id;
        this.code = code;
        this.name = name;
    }

    ...
    @JsProperty
    public double getId() {
        return id;
    }

    @JsProperty
    public double setId(double id) {
        this.id = id;
    }
    ...
}

Then in my GWT application I'd like to do this:

    public class JsTypes {

        public static native <T> T getJsTypeObject(JavaScriptObject result)/*-{
            return result;
        }-*/;
    }

    //called after REST response from server
    private void onCallback(String json) {
Console.log("JSON:" + json);
JavaScriptObject result = JsonUtils.safeEval(json);
User user = JsTypes.getJsTypeObject(result);
        ...
        //do something with user
   }

Any ideas or suggestions would be greatly appreciated.

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