我正在通过以下方式将GWT方法导出到本地javascript:
public class FaceBookGalleryEntryPoint implements EntryPoint {
@Override
public void onModuleLoad() {
FacebookGallery facebookGallery = new FacebookGallery();
RootPanel.get().add(facebookGallery);
initLoadGallery(facebookGallery);
}
private native void initLoadGallery(FacebookGallery pl) /*-{
$wnd.loadGallery = function (galleryId) {
[email protected]::loadGallery(Ljava/lang/String;)(galleryId);
};
}-*/;
}
在主机页面中,我正在尝试调用它:
<html>
<head>
<title>Facebook image gallery</title>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript" src="/fbg/fbg.nocache.js"></script>
<h1>Facebook gallery test</h1>
<script type="text/javascript">
$(document).ready(function() {
loadGallery('blargh');
});
</script>
</body>
</html>
不幸的是,当调用document.ready回调时,该函数尚未定义。从Firebug控制台手动执行时,该功能可以正常运行。
我可以每50毫秒执行一次轮询,直到找到该名称的已定义函数,但这似乎是一种可怕的方法。
如何在模块加载时以及因此功能可用时得到通知?
我将尝试在主页中定义一个回调函数,并在onModuleLoad()方法末尾从GWT调用它。
主页功能:
<script type="text/javascript">
function onGwtReady() {
loadGallery('blargh');
};
</script>
GWT:
public void onModuleLoad() {
FacebookGallery facebookGallery = new FacebookGallery();
RootPanel.get().add(facebookGallery);
initLoadGallery(facebookGallery);
// Using a deferred command ensures that notifyHostpage() is called after
// GWT initialisation is finished.
Scheduler.get().scheduleDeferred(new Command() {
public void execute() {
notifyHostpage();
}
}
}
private native void notifyHostpage() /*-{
$wnd.onGwtReady();
}-*/;