Saturday, November 18, 2017

Re: JsInterop Shared Model + REST API



On Saturday, November 18, 2017 at 9:52:09 AM UTC+1, 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.

If you're going to use the type from a JSON.parse(), you might want to use a native type mapping to a simple JS Object, rather than exporting a to a JS class that you'll never actually use (and that could cause ClassCastException⋅s at runtime).

@JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "Object")
public class User {
 
@JsProperty private double id;
 
@JsProperty private String code;
 
@JsProperty private String name;


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

 
}


 
// Didn't check if you still need the 0-args constructor


 
@JsOverlay public double getId() { return id; }
 
@JsOverlay public void setId(double id) { this.id = id; }


 

}


User user = (User) JsonUtils.<JavaScriptObject>safeEval(json);
or use elemental2-core & jsinterop-base:
User user = Js.cast(Global.JSON.parse(json));
or a custom JsInterop mapping for JSON.parse:
@JsMethod(namespace = "JSON") private static native User parse(String json);
User user = parse(json);


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