我怎样才能从Java匿名的JavaScript对象传递给JavaScript的GWT?
-
12-09-2019 - |
题
我创建一个GWT包装一轮JavaScript库。一个的JavaScript函数需要一个匿名对象作为参数e.g:
obj.buildTabs({ hide: true, placeholder: 'placeholder' });
在Java方面如何创建这种类型的JavaScript对象,并把它传递给我的本地实现?
目前,在Java端我有:
public void buildTabs(TabConfiguration config) {
// ?
}
private native void buildTabs(?) /*-{
$wnd.NAMESPACE.lib.buildTabs(?);
}-*/;
任何指针理解,谢谢。
解决方案
如果你确切地知道应该用什么参数,你可以做以下的(后除去额外的新行::)
private native void buildTabs(TabConfiguration config) /*-{
$wnd.NAMESPACE.lib.buildTabs({hide:
config.@com.yournamehere.TabConfiguration::
getHide()(),
placeholder:
config.@com.yournamehere.TabConfiguration::
getPlaceholder()()});
}-*/;
public native void bar(JSNIExample x, String s) /*-{
// Call instance method instanceFoo() on this
this.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);
// Call instance method instanceFoo() on x
x.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);
// Call static method staticFoo()
@com.google.gwt.examples.JSNIExample::staticFoo(Ljava/lang/String;)(s);
// Read instance field on this
var val = this.@com.google.gwt.examples.JSNIExample::myInstanceField;
// Write instance field on x
x.@com.google.gwt.examples.JSNIExample::myInstanceField = val + " and stuff";
// Read static field (no qualifier)
@com.google.gwt.examples.JSNIExample::myStaticField = val + " and stuff";
}-*/;
不隶属于 StackOverflow