Wednesday, November 15, 2017

Re: Dynamic values in json parsing with GWT



On Wednesday, November 15, 2017 at 11:19:56 AM UTC+1, Jens wrote:

Thanks for the early response. Can you give an example of getData(String key) method.

Sure, assuming your values are only strings you can do

public native String getData(String key) /*-{
  return this.data[key];
}-*/
;

If your values can also be number, boolean, JS Object you need additional methods, possibly also doing some type checking. 

Given that JSNI won't work in future GWT I would really take a look at jsinterop.base.JsPropertyMap which is a generic class to work on JavaScript objects.

@JsType(isNative = true, name = "Object", namespace = JsPackage.GLOBAL)
public class Result {
 
String[] columns; // or JsArrayLike<String>
 
JsPropertyMap<String> data;
}

That should be enough (and much much simpler/shorter than with JSNI).

Migrating a JSNI getData(String):String to JsInterop would be quite easy too:

@JsType(isNative = true, name = "Object", namespace = JsPackage.GLOBAL)
public class Result {
 
@JsProperty public native String[] getColumns();
 
@JsOverlay public String getData(String key) {
   
return getData().get(key);
 
}
 
@JsProperty private native JsPropertyMap<String> getData();
}



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