我正在 GWT 中制作一个仅客户端的应用程序,它使用 WebGL 将后处理效果应用于图像。我已将 InputElement
添加到这样的文件类型文档中。
InputElement inputElement = InputElement.as(Document.get().createFileInputElement());
inputElement.setAccept("image/*");
Document.get().getBody().appendChild(inputElement);
然后我想向它添加一个 onchange 监听器,由于 GWT 不支持这一点,所以我编写了这个 JSNI 方法来做到这一点。
private native void registerOnChange(Main object, InputElement element) /*-{
if (!$wnd.update)
$wnd.update = $entry(object.@com.shc.cartoonizer.client.Main::updateImage(*)(element));
element.onchange = $wnd.update;
}-*/;
问题是,这会在文件更改时引发 JS TypeError。这是控制台中抛出的错误。
Uncaught TypeError: Cannot read property 'apply' of undefined
apply_0_g$ @ Impl.java:247
entry0_0_g$ @ Impl.java:306
(anonymous function) @ Impl.java:72
我做错了什么吗?
最佳答案
$entry(object.@com.shc.cartoonizer.client.Main::updateImage(*)(element))
$entry
将函数作为输入并返回函数,但您传递 updateImage
函数的结果。
因为 $entry
在传入的函数上调用 apply
,并且您的 updateImage
可能有一个 void
返回类型(这意味着 JS 中的函数将隐式返回 undefined
),您会收到 Cannot read property 'apply' of undefined
错误。
解决方案是将调用 updateImage
的函数传递给 $entry
:
$entry(function() {
object.@com.shc.cartoonizer.client.Main::updateImage(*)(element);
})
话虽如此,您有什么理由不使用 FileUpload
小部件和 addChangeHandler
吗?
关于java - 更改 GWT 客户端上的文件输入元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33316708/