gpt4 book ai didi

java - JSNI 包装对象

转载 作者:行者123 更新时间:2023-12-01 14:29:54 25 4
gpt4 key购买 nike

下面是访问 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 对象包装器?

最佳答案

gwtquery是 GWT 的一个很好的补充,它有很多帮助器,可以方便地与 javascript 交互,而无需编写 jsni 等。

在您的情况下,您的代码可能类似于:

// 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");

关于java - JSNI 包装对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16932612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com