我试图将大部分原生JavaScript代码从JSNI方法中移出并转换为脚本,并且只是利用本地JSNI方法来调用这些外部方法。GWT:可以从外部JavaScript代替JSNI调用Java方法吗?
现在我遇到了一个点击处理程序的问题。当用户点击某个元素时,JSNI方法会执行一些基于JQuery的动画,然后在回调中调用Java方法。一个简单的例子是:
public native void attachClickHandler(SomeCustomPanel customPanel) /*-{
$wnd.jQuery("#theElement").click(function() {
// some JQuery animation logic here...
$wnd.jQuery("#theElement").animate({ top: "500px" }, 500, function() {
[email protected]::doSomething()();
});
// some other code here...
});
}-*/;
此代码的工作原理。它按预期进行编译和工作。我想将其移入外部JavaScript中。我尝试了以下。我把这个在外部JavaScript:
function attachClickAction(customPanel) {
$("#theElement").click(function() {
// other stuff...
$("#theElement").animate({ top: "500px" }, 500, function() {
[email protected]::doSomething()();
});
// other stuff...
});
}
而修改这样的原生功能:
public native void attachClickHandler(SomeCustomPanel customPanel) /*-{
$wnd.attachClickAction(customPanel);
}-*/;
但不正确。 JavaScript文件甚至不会加载,因为这是不正确的JavaScript。 (丁目的开发工具给我的错误消息“未捕获的SyntaxError:意外的标识符”。)
有没有办法来调用外部JavaScript文件,不 Java方法从JSNI方法?
我在GWT 2.4中,如果它很重要。
注意:您应该使用'$ entry()'调用'customPanel ... doSomething()()'。 –
@avasopht - 为什么?我从来没有见过这样的构造。你可以解释吗? –
它围绕某些try/catch语句进行调用,以避免GWT/javascript中的Java代码异常将达到纯JavaScript代码空间。 –