我以以下方式将 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) {
pl.@com.example.fbg.client.FacebookGallery::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 毫秒执行一次轮询,直到找到按该名称定义的函数,但这似乎是一种可怕的方法。
如何在加载模块以及功能可用时收到通知?