简体   繁体   English

GWT和JSNI:第三方库集成

[英]GWT and JSNI: third-party library integration

I'm trying to integrate Opentip into a GWT project. 我正在尝试将Opentip集成到GWT项目中。 Since some of my widgets are loaded from Java instead of the HTML, it seems like I'll have to use JSNI to get those tooltips bound properly. 由于我的某些小部件是从Java而不是HTML加载的,因此我似乎不得不使用JSNI来正确绑定那些工具提示。 Here's what I've done so far: 到目前为止,这是我所做的:

  1. Put the relevant JS/CSS declarations before GWT's nocache.js in my HTML file. 将相关的JS / CSS声明放在HTML文件中GWT的nocache.js之前。 (I also tried duplicating the JS loading by using ScriptInjector , but that didn't do any good, so I ripped it out as redundant. (我还尝试使用ScriptInjector复制JS加载,但这没有任何用处,因此我将其删除为多余的。
  2. Written a JSNI method to instantiate the tooltips: 编写了JSNI方法来实例化工具提示:

     private native void initControlTooltips() /*-{ var headerText = "Tooltip text"; new $wnd.Opentip($("#tooltipTrigger"), headerText); // more tooltips... }-*/ 

I've tried different variants of the above method, but I've read that this is the way it's supposed to work (Slide 20 here , though I can't link directly to the slide). 我尝试了上述方法的不同变体,但是我已经读到了这是应该起作用的方式( 此处是幻灯片20,尽管我不能直接链接到幻灯片)。 So far I've had no luck, and this most recent attempt, which seems the most correct to me, has actually stopped the rest of my GWT module from loading (after the tooltip instantiation call), as if I have a syntax error somewhere, though the console isn't reporting anything. 到目前为止,我还没有运气,最近一次的尝试(对我来说似乎是最正确的)实际上已经阻止了我的GWT模块其余部分的加载(在工具提示实例化调用之后),好像我的某个语法错误,尽管控制台没有报告任何内容。 Any ideas as to what I'm doing wrong? 关于我在做什么错的任何想法吗? I'm relatively new to both GWT and JS, so I'm hoping this is just a stupid beginner's mistake. 我对GWT和JS都比较陌生,所以我希望这只是一个愚蠢的初学者的错误。

The one thing I haven't tried yet is coding up an Overlay object , partially because I can't see from the docs how to properly wrap a JS constructor. 我还没有尝试过的一件事是编写一个Overlay对象 ,部分原因是我从文档中看不到如何正确包装JS构造函数。 If that's the solution here, some pointers on how to do that would be welcome. 如果这是解决方案,那么欢迎提供一些有关如何执行此操作的指示。 As a reference, the constructor I'd be using (from the Opentip documentation) is: 作为参考,我将使用的构造函数(来自Opentip文档)是:

new Opentip("#my-trigger-element", "Optional content", "Optional title", { ...options... });

Thanks for any help; 谢谢你的帮助; this doesn't seem like it should be that tough of a problem, but so far my inexperience has foiled my efforts. 这似乎不应该是一个棘手的问题,但是到目前为止,我的经验不足挫败了我的努力。

Update 更新资料

After several hours of cursing and parameter fiddling, I've resolved this, so don't waste any time trying to help me debug. 经过数小时的咒骂和参数摆弄,我已经解决了这个问题,所以不要浪费任何时间尝试帮助我进行调试。 I'll write up an answer a little later today when I get a bit of free time. 我会在稍晚的空闲时间写一个答案。

So you've got your web.gwt.xml , your module, and your EntryPoint all set up. 这样就完成了web.gwt.xml ,模块和EntryPoint设置。 If you don't have those things, this isn't the answer for you. 如果您没有这些东西,那么这不是您的答案。 The documentation isn't a bad place to start. 该文档不是一个不好的起点。 This answer will use Opentip as an example, but I don't see why it wouldn't work for others too; 这个答案将以Opentip为例,但是我不明白为什么它对其他人也无效。 specifically, though, this is geared for if you have widgets set up solely in GWT (not accessible from your HTML) and need to bind your external JS to them to enable some functionality. 但是,特别是,如果您仅在GWT中设置了小部件(无法从HTML进行访问),并且需要将外部JS绑定到它们以启用某些功能,则适用于这种情况。

  1. Crack open your web.gwt.xml and make sure you're including your library's source in the entry for the module where you want to use it. 破解您的web.gwt.xml ,并确保在要使用它的模块的条目中包含库的源代码。 This means that you don't have to declare it in your page's static HTML if you don't specifically need it there as well. 这意味着,如果您也不特别需要在页面的静态HTML中声明它,则无需在页面的静态HTML中声明它。

     <script src="../js/opentip/opentip-jquery.min.js"/> 

    (Note the ../ - this is a relative path from where your GWT-generated HTML file ends up on your server, and it has to be accessible by your web server.) (请注意../这是GWT生成的HTML文件最终在服务器上的相对路径,必须由Web服务器访问。)

  2. In the module class where you want to use the library, rig up a JSNI method, calling your library constructor like so: 在要使用该库的模块类中,装配一个JSNI方法,如下所示调用您的库构造函数:

     private native void initTooltip() /*-{ new $wnd.Opentip($doc.getElementById("tooltipTrigger"), "Tooltip text"); }-*/; 

    (Note the $wnd for referring to the top-level window and the $doc for referring to the document. Prepending $wnd. works for referring to any global variables declared/assigned in external JS files.) (请注意, $wnd用于引用顶级窗口, $doc用于引用文档。在$wnd. wnd之前$wnd.用于引用在外部JS文件中声明/分配的任何全局变量。)

No fancy Overlay types or ScriptInjector calls needed. 不需要花哨的Overlay类型或ScriptInjector调用。 I did have one other problem while working this out - my call to getElementById() was coming back null - this turned out to be because I was calling my JSNI method before the relevant object had been added to the DOM during onModuleLoad() . 我也有一个问题,其他工作时这一点-我调用getElementById()回来了null -这竟然是因为我打电话给我的JSNI方法之前在相关对象已被添加到DOM onModuleLoad()

Like I thought initially, this turned out to be a simple beginner's mistake; 就像我最初认为的那样,这原来是一个简单的初学者的错误。 hope this can help someone else trying to piece together other answers to similar questions into a workable solution for their problem. 希望这可以帮助其他试图将对类似问题的其他答案组合在一起的人,以解决他们的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM