【问题标题】:Native Javascript method in GWTGWT 中的原生 Javascript 方法
【发布时间】:2010-11-02 04:30:20
【问题描述】:

我的一个 GWT Java 类中有一个本地 Javascript 方法,但是我无法从本地 Javascript 代码调用我的 Java 方法。我试图尽可能地关注this,但我无法让它发挥作用。我编译它并在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 {
                this.@com.proprintsgear.design_lab.client.ValueBox::increaseValue()();
            }
            this.@com.proprintsgear.design_lab.client.ValueBox::fireChange()();
        }
    }

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

【问题讨论】:

    标签: java javascript gwt native


    【解决方案1】:

    我找到了更好的方法。它类似于您在 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) {
      that.@org.dartlang.gwtapplication.client.GwtApplication::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);
    }
    

    【讨论】:

      【解决方案2】:

      在我过去完成的所有代码中,我从来没有使用'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 {
                              this.@com.proprintsgear.design_lab.client.ValueBox::increaseValue()();
                      }
                      this.@com.proprintsgear.design_lab.client.ValueBox::fireChange()();
              }
          }
      
          var box=$doc.getElementById(id);
          box.addEventListener("mouseout",mouseOutHandler,false);
          box.addEventListener("mouseover",mouseOverHandler,false);
      }-*/;
      

      到这里:

      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 {
                              instance.@com.proprintsgear.design_lab.client.ValueBox::increaseValue()();
                      }
                      instance.@com.proprintsgear.design_lab.client.ValueBox::fireChange()();
              }
          }
      
          var box=$doc.getElementById(id);
          box.addEventListener("mouseout",mouseOutHandler,false);
          box.addEventListener("mouseover",mouseOverHandler,false);
      }-*/;
      

      【讨论】:

      • 完美!这是有道理的,但我希望它在 GWT 网站上得到更好的记录。
      猜你喜欢
      • 2013-01-01
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-06
      • 1970-01-01
      相关资源
      最近更新 更多