题
我导出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回调时,该功能还没有定义。当从萤火虫手动执行控制台功能工作得很好。
我可以执行一些投票站每隔50毫秒,直到我找到这个名字定义的功能,但它似乎是一个可怕的方式。
如何能当模块被加载我得到通知,因此,当该功能有效?
解决方案
我想尝试在hostpage定义的回调函数,并在onModuleLoad()方法的端部从GWT调用它。
Hostpage功能:
<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.
DeferredCommand.addCommand(new Command() {
public void execute() {
notifyHostpage();
}
}
private native void notifyHostpage() /*-{
$wnd.onGwtReady();
}-*/
不隶属于 StackOverflow