简体   繁体   English

GWT JSNI方法暴露

[英]GWT JSNI method exposure

I have a question regarding using JSNI to expose one of my GWT methods. 我有一个关于使用JSNI公开我的GWT方法之一的问题。

I have am trying to expose a method in one of my GWT classes that fires a message to other UI components. 我正在尝试在我的GWT类之一中公开一个方法,该方法将消息发送到其他UI组件。

In my application entry point I expose it with 在我的应用程序入口点,我用

   public native void exportMethods() /*-{
        $wnd.fireFoo=$entry(this.@myPackage.class_a::fireFoo(Ljava/lang/String;));
   }-*/;

class_a updates one of its fields, and then calls a static method from class_b. class_a更新其字段之一,然后从class_b调用静态方法。

   public class class_a{

       private String last_msg;

       public void fireFoo(String msg){
           this.last_msg = msg;
           class_b.foo(msg);
       }
   }

class_b calls many other classes and static methods to process the msg class_b调用许多其他类和静态方法来处理味精

   public class class_b{
       public static void foo(String msg){
       ...//creates a message object and sends it to UI components
       class_c.foo2(msg);
       ...
       }

   }

Basically, all I need to do is pass off a string and call a GWT method. 基本上,我需要做的就是传递一个字符串并调用GWT方法。 The method (when called from GWT and not handwritten JS) works perfectly. 该方法(当从GWT而不是手写JS调用时)效果很好。 I have also verified that the exposed method will fire a simple alert. 我还验证了公开的方法将触发简单的警报。 I believe the problem lies in calling the other classes' methods. 我相信问题在于调用其他类的方法。

I get: "(TypeError) Unable to get property 'hv' of undefined or null reference description." 我得到:“(TypeError)无法获取未定义或空引用描述的属性'hv'。”

Is there a way to pass off a string to the original GWT method without having to go an expose the countless other methods that it will eventually run through? 有没有一种方法可以将字符串传递给原始的GWT方法,而不必暴露最终将要运行的无数其他方法?

Just like in JavaScript, this.@myPackage.class_a::fireFoo(Ljava/lang/String;) is a reference to a method, but it doesn't bind that method to the this object at that time. 就像在JavaScript中一样, this.@myPackage.class_a::fireFoo(Ljava/lang/String;)是对方法的引用,但当时并未将该方法绑定this对象。 The this from inside the method will be determined at the time the function is called, not the time the reference is retrieved. 方法内部的this将在调用函数时确定, 而不是在检索引用时确定。

You thus need a delegate : 因此,您需要一个代表

var that = this;
$wnd.fireFoo = $entry(function(s) {
  that.@myPackage.class_a::fireFoo(Ljava/lang/String;)(s);
});

If it indeed works in DevMode as you say, then it's a bug in DevMode. 如果确实按照您所说的在DevMode中工作,那么这是DevMode中的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM