如何将javascript对象传递给GWT方法并解析结果 [英] How to pass javascript object to GWT method and parse result

查看:64
本文介绍了如何将javascript对象传递给GWT方法并解析结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个GWT方法:

public static native JavaScriptObject getJsValue() /*-{
    var res = $wnd.product; 
    return res;
}-*/;

这是HTML/JS部分:

This is the HTML/JS part:

<script type="text/javascript" language="javascript">
    var product = products({id:1}).first(); 
</script> 
<!-- GWT -->
<script type="text/javascript" language="javascript" src="app/app.nocache.js"></script> 

对象product在Firebug中看起来像这样:

The object product looks like this in Firebug:

Object { id=1, categoryid=0, name="Sample Product", more...}

然后,

Object obj = getJsValue();  // what cast? 

但是,如何解析结果值以获取诸如产品ID之类的字段值?

However, how can I parse the resulting value to get the field values like the product id, etc.?

推荐答案

如果我正确理解了这个问题,我会使用覆盖类型,例如:

If I have correctly understood the question, I'd use an overlay type, something like:

public class ProductJso extends JavaScriptObject {
  protected ProductJso() {}
  public final native int getId() /*-{ 
    return this.id;
  }-*/;
  public final native int getCategoryId() /*-{
    return this.categoryid;
  }-*/;
  public final native String getName() /*-{
    return this.name;
  }-*/;
  // And so on...
}

然后修改您JSNI以返回实际的JSO类型

Then modify you JSNI to return the actual JSO type

public static native ProductJso getJsValue() /*-{
  return $wnd.product; 
}-*/;

您有了主意,另请参阅 https ://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsOverlay?hl = it#example-json

You get the idea, see also https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsOverlay?hl=it#example-json

这篇关于如何将javascript对象传递给GWT方法并解析结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆