我通过以下方式将 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();
}-*/;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)