Q: So what's the trick?
Pretty easy actually. The problem is that jsinterop assumes that js libraries are loaded in TOP_WINDOW ($wnd) and when this is not true it fails to find the functions to call them.
So let's give jsinterop a clue.
1) First of all create a js variable in top window that holds GWT iframe
public static native void jsInit() /*-{
$wnd.__gwtIFrame = $wnd.document.querySelectorAll('iframe#YourGWTApplicationID')[0].contentWindow;
}-*/;
2) The load the jslibrary in GWT iframe
package com.common.lib.gwt.client.js.resources;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.ScriptInjector;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.TextResource;
public interface JQueryResources extends ClientBundle {
public static final JQueryResources INSTANCE = GWT
.create(JQueryResources.class);
public static JQueryResources load() {
// loads by default in GWT Iframe
ScriptInjector.fromString(INSTANCE.js().getText()).inject();
return INSTANCE;
}
@Source("jquery.min.js")
TextResource js();
}
3) Then in jsinterop define __gwtIFrame as the namespace
// instead of that
// @JsMethod(namespace = JsPackage.GLOBAL)
// do that
@JsMethod(namespace = "__gwtIFrame")
public static native <T extends JQuerySelection> T $(String selector);
Now you can call $ from GWT Iframe
Hope that helps somebody.
Any comments on the approach?