以下是访问JS方法的常用方法:
public class JSNIHelper {
public static native void errorNotify(String _title, String _text) /*-{
$wnd.$.pnotify({
title: _title,
text: _text,
type: 'error'
})
}-*/;
}然而,在JSNI之上是否有一个“对象包装器”,以便以更像Java对象的方式访问Javascript:
JSNIWrapper().call("$").method("pnotify")
.set("title", title)
.set("text", text)
.set("type", type).now();我不完全确定什么是最好的实现,我不是JS专家。所以我的问题是,是否存在任何现有的JSNI对象包装器?
发布于 2013-06-05 18:35:56
gwtquery是GWT的一个很好的补充,它有大量的助手,可以在不编写jsni的情况下方便地与javascript进行交互。
在您的示例中,您的代码可能类似于:
// Create a JSO and set the properties
Properties p = Properties.create();
p.set("title", title);
p.set("text", text);
p.set("type", type);
// Get the JSO which has the function we want to call
JavaScriptObject $ = JsUtils.prop(window, "$");
// Call the JS method and get the result (null if the native
// js function returns void
Object ret = JsUtils.runJavascriptFunction($, "pnotify", p);顺便说一句,您提出的链接语法非常有意义,所以您可以建议将此功能作为对gquery Properties对象的增强。类似于:
$$().set("title", title).set("text", text).set("type", type).call(window, "$.pnotify");https://stackoverflow.com/questions/16932612
复制相似问题