2013-04-10 98 views
2

我有一個關於使用JSNI來公開我的一個GWT方法的問題。GWT JSNI方法曝光

我試圖在我的一個GWT類中公開一個方法,該方法將消息發送到其他UI組件。

在我的應用程序入口點我

public native void exportMethods() /*-{ 
     $wnd.fireFoo=$entry([email protected]_a::fireFoo(Ljava/lang/String;)); 
    }-*/; 

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調用許多其他類和靜態方法來處理味精

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

    } 

基本上,所有我需要做的是通關的字符串,並調用GWT方法。該方法(當從GWT調用而不是手寫JS時)完美地工作。我還證實,暴露的方法會觸發一個簡單的警報。我相信問題在於調用其他類的方法。

我得到:「(TypeError)無法獲取未定義或空引用說明的屬性'hv'。」

有沒有辦法將一個字符串傳遞給原始的GWT方法,而不必暴露它將最終運行的無數其他方法?

回答

5

就像在JavaScript中一樣,[email protected]_a::fireFoo(Ljava/lang/String;)是對方法的引用,但它不會綁定到this對象那個時候。來自方法內部的this將在函數被調用時確定,而不是檢索到引用的時間。

您因而需要一個委託

var that = this; 
$wnd.fireFoo = $entry(function(s) { 
    [email protected]_a::fireFoo(Ljava/lang/String;)(s); 
}); 

如果在DevMode的就像你說的,那麼它在DevMode的一個bug確實有效。