从手写的 Javascript 调用 GWT 方法
我正在使用 GWT 构建一个 Web 应用程序,我希望用户能够通过某种 Javascript API 来扩展其功能,这些 API 可以与用 GWT 编写的应用程序核心进行交互。基本上,这个 JS API 将映射到我的一些 GWT 内部方法来完成实际工作。
因此,为了实现这一目标,我在 GWT JSNI 官方文档中读到了一个技巧,该技巧包括从 GWT 创建全局 JS 变量并为其分配对实际 GWT 方法的调用。然后我会通过这个变量在我手写的 JS 代码中进行调用。
不幸的是我无法使它工作(该示例包含语法错误,我不确定它是否已经过测试)。经过更多研究,我在这个网站上发现了一个类似的解决方案,它为 JS var 分配一个进行调用的匿名函数。它看起来像这样:
/*GWT code that assigns the anonymous function*/
private static native void loadWrapper() /*-{
$wnd.showMessage =
$entry(@com.Glob3Mobile.client.TestApi::msgBox());
}-*/;
public static void msgBox() {
Window.alert("hello");
}
这个解决方案看起来很有前途,但我还没能让它发挥作用。当我执行“window.showMessage();”时在我手写的 JS 代码中它不起作用。我发现了这些问题:
- window.showMessage 似乎已创建,但 JS 控制台说它不是一个函数,即使我检查了 dom 树并验证该变量确实包含一个函数。
- 看来我总是必须附加“窗口”。前缀,因为 var 是在 window 对象内创建的。这是一个相当令人担忧的问题,因为强迫用户每次想要调用函数时都这样编写确实很乏味。
如果有人知道这些问题的解决方案(或者实现我想做的事情的替代方法),我将非常感激听到。提前致谢。
Im building a web application with GWT and I want to give users the ability to extend its functionality by some sort of Javascript API that would interact with the app core written in GWT. Basically, this JS API would map to some of my GWT internal methods that would do the actual job.
So, to achieve this, i read on GWT JSNI official documentation a trick that consists in creating a global JS variable from GWT and assigning it a call to the actual GWT method. Then i would make the call in my handwritten JS code through this variable.
Unfortunately I couldnt make it to work (the example contained syntax errors son im not sure that it has even been tested). Doing more research, i found on this site a similar solution that assigns the JS var an anonymous function that makes the call. It looks like this:
/*GWT code that assigns the anonymous function*/
private static native void loadWrapper() /*-{
$wnd.showMessage =
$entry(@com.Glob3Mobile.client.TestApi::msgBox());
}-*/;
public static void msgBox() {
Window.alert("hello");
}
This solution looks promising but I havent been able to make it to work. When i do "window.showMessage();" in my handwritten JS code it wont work. I have found these issues:
- window.showMessage seems to be created but JS console says that its not a function, even though i inspected the dom tree and verified that the variable does contains a function.
- It seems like I always have to append the "window." preffix, since the var is being created within the window object. This is a considerably concerning issue, cause forcing users to write like this every time they want to call a function is really tedious.
If anybody knows the solution to these issues (or an alternative approach to achieve what im trying to do), id be very grateful to hear. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你需要做一些额外的工作。因为您描述的方法仅在编译时有效。编译后,GWT 方法名称消失了,因为它们被混淆了。但是有一个库可以帮助您解决此问题:gwt-exporter
You need to do some extra work. Because the method you described works only at compile time. After compilation the GWT method names are gone, because they are obfuscated. But there is a library that can help you with this: gwt-exporter