Re: JsInterop Shared Model + REST API

2017-11-21 Thread Chris L
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.
>
> https://github.com/GWTReact/gwt-interop-utils/blob/master/DOCUMENTATION.md
>
>
>
> 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 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+unsubscr...@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.


Re: JsInterop Shared Model + REST API

2017-11-18 Thread Thomas Broyer


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 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.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+unsubscr...@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.


Re: JsInterop Shared Model + REST API

2017-11-18 Thread Paul Stockley
gwt-interop-utils shows one way of doing it. This assumes you are OK using 
intermediate collection types.

https://github.com/GWTReact/gwt-interop-utils/blob/master/DOCUMENTATION.md



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 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+unsubscr...@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.


JsInterop Shared Model + REST API

2017-11-18 Thread Chris Lapes
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 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+unsubscr...@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.