GWT 中的原生 Javascript 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/981924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:51:21  来源:igfitidea点击:

Native Javascript method in GWT

javajavascriptgwtnative

提问by DLH

I have a native Javascript method in one of my GWT Java classes, but I'm having trouble calling my Java methods from the native Javascript code. I tried to follow thisas closely as I could, but I can't get it to work. I compiled it and ran it in Firefox, and the error console said "Error: this.lc is not a function". I tried changing all the methods to public, but that didn't seem to make a difference. What am I doing wrong?

我的一个 GWT Java 类中有一个本机 Javascript 方法,但是我无法从本机 Javascript 代码调用我的 Java 方法。我试图尽可能密切地遵循一点,但我无法让它发挥作用。我编译它并在Firefox中运行它,错误控制台说“错误:this.lc不是函数”。我尝试将所有方法更改为public,但这似乎没有什么区别。我究竟做错了什么?

package com.proprintsgear.design_lab.client;
...
public class ValueBox extends HorizontalPanel {
...
private void fireChange() {
    ...
}

private void increaseValue() {
    ...
}

private native void addNativeMouseWheelListener(String id) /*-{
    function mouseOverHandler(e) {
        $wnd.addEventListener("DOMMouseScroll", scrollWheelMove, false);
    }

    function mouseOutHandler(e) {
        $wnd.removeEventListener("DOMMouseScroll", scrollWheelMove, false);
    }

    function scrollWheelMove(e) {
        if ($wnd.event || $wnd.Event) {
            if (!e) e = $wnd.event;
            if (e.wheelDelta <= 0 || e.detail > 0 ) {
                $wnd.alert("DOWN");
            } else {
                [email protected]_lab.client.ValueBox::increaseValue()();
            }
            [email protected]_lab.client.ValueBox::fireChange()();
        }
    }

    var box=$doc.getElementById(id);
    box.addEventListener("mouseout",mouseOutHandler,false);
    box.addEventListener("mouseover",mouseOverHandler,false);
}-*/;

采纳答案by rustyshelf

In all the code I've done in the past, I've never used 'this' to identify my class, I have passed the class in.

在我过去完成的所有代码中,我从未使用“this”来标识我的班级,而是将班级传入。

Eg: Change this:

例如:改变这个:

private native void addNativeMouseWheelListener(String id) /*-{
    function mouseOverHandler(e) {
        $wnd.addEventListener("DOMMouseScroll", scrollWheelMove, false);
    }

    function mouseOutHandler(e) {
        $wnd.removeEventListener("DOMMouseScroll", scrollWheelMove, false);
    }

    function scrollWheelMove(e) {
        if ($wnd.event || $wnd.Event) {
                if (!e) e = $wnd.event;
                if (e.wheelDelta <= 0 || e.detail > 0 ) {
                        $wnd.alert("DOWN");
                } else {
                        [email protected]_lab.client.ValueBox::increaseValue()();
                }
                [email protected]_lab.client.ValueBox::fireChange()();
        }
    }

    var box=$doc.getElementById(id);
    box.addEventListener("mouseout",mouseOutHandler,false);
    box.addEventListener("mouseover",mouseOverHandler,false);
}-*/;

To this:

对此:

private native void addNativeMouseWheelListener(ValueBox instance, String id) /*-{
    function mouseOverHandler(e) {
        $wnd.addEventListener("DOMMouseScroll", scrollWheelMove, false);
    }

    function mouseOutHandler(e) {
        $wnd.removeEventListener("DOMMouseScroll", scrollWheelMove, false);
    }

    function scrollWheelMove(e) {
        if ($wnd.event || $wnd.Event) {
                if (!e) e = $wnd.event;
                if (e.wheelDelta <= 0 || e.detail > 0 ) {
                        $wnd.alert("DOWN");
                } else {
                        [email protected]_lab.client.ValueBox::increaseValue()();
                }
                [email protected]_lab.client.ValueBox::fireChange()();
        }
    }

    var box=$doc.getElementById(id);
    box.addEventListener("mouseout",mouseOutHandler,false);
    box.addEventListener("mouseover",mouseOverHandler,false);
}-*/;

回答by Shannon -jj Behrens

I found a better way. It's similar to what you do in JavaScript, where you set "var that = this". Using this approach, you don't have to pass this to listenForPostMessage():

我找到了更好的方法。它类似于你在 JavaScript 中所做的,在那里你设置“var that = this”。使用这种方法,您不必将其传递给 listenForPostMessage():

protected native void postMessage(String msg) /*-{
  $wnd.postMessage(msg, "*");
}-*/;

private final native void listenForPostMessage() /*-{
  var that = this;
  $wnd.addEventListener("message", function(msg) {
  [email protected]::onPostMessage(Ljava/lang/String;Ljava/lang/String;)(
    msg.data, msg.origin);
  });
}-*/;

private void onPostMessage(String data, String origin) {
  Label msgLabel = new Label();
  msgLabel.setText("GWT received a postMessage: Data: " +
      data + " Origin: " + origin);
  mainPanel.add(msgLabel);
}