2012-11-09 47 views
1

我試圖在GWT應用中實現Mozilla的Persona。下面是一個虛擬的程序,我設置爲測試它的部分代碼:GWT JSNI方法調用失敗,但沒有錯誤

public class OpenId implements EntryPoint { 

private native void callWatch(String email) 
/*-{ 
    $wnd.navigator.id.watch({ 
     loggedInUser: email, 
     onlogin: function(assertion){ 
      $wnd.alert("Calling method"); 
      [email protected]::processLogin(Ljava/lang/String;)(assertion); 
      $wnd.alert("Called Java Method"); 
     }, 
     onlogout: function(){alert("Logged Out!");} 
    }); 
}-*/; 

private void processLogin(String assertion){ 
    Window.alert("Logged in!"); 
    personaStatus.setText("Log In Complete."); 
} 
} 

當我打電話callWatch方法,只有「調用方法」警告框出現。其他人都不會被稱爲。因此,出於某種原因,代碼似乎停在JSNI呼叫的正下方,並位於第一條警報的下方。但是在開發模式中沒有錯誤。

我不明白爲什麼processLogin方法沒有被調用。

我以爲我正確地跟着Google's Documentation

我也嘗試寫

[email protected]::processLogin(Ljava/lang/String;)(assertion); 

[email protected][email protected]由於this post

我不知道還有什麼可以嘗試的。

+0

看起來有可能是這樣的斷言varible一個porblem ...沒有 '$ wnd.alert(斷言);'顯示一個字符串。如果是這樣你嘗試傳遞一個靜態字符串到你的processLogin函數。 – Stefan

回答

4

變量this指向立即包圍它的函數,在這種情況下,您的onlogin JavaScript函數。你需要使用一個臨時變量that(典型的JavaScript的成語,順便說一句)

private native void callWatch(String email) 
/*-{ 
    var that = this; 
    ... 
    onlogin: function(assertion){ 
     [email protected] 

然後,用理想$entry(...),所以你會看到錯誤消息,如果您已註冊的UncaughtExceptionHandler的。

參見:https://stackoverflow.com/a/5235580/291741

+0

這樣做。謝謝! – gridDragon