从JSNI方法中调用Java方法

问题描述:

我想从我的JSNI方法中调用java方法。我没有收到任何类型的错误,但Window.alert()永远不会被调用。从JSNI方法中调用Java方法

package com.mywebsite.myapp.client; 

public class MyApp implements EntryPoint(){ 
/// Other stuff.... 
    public native void getToServer(String trainerName)/*-{ 
    $wnd.$.get("http://testdastuff.dev/trainerstats", { trainer: trainerName}) 
    .fail(function() { 
     $wnd.console.log("error"); 
    }) 
    .done(function(data) { 
      if(data == "noData"){ 
      alert("NO DATA"); 
      [email protected]::testJSNI()();   
    } 
    }); 
    }-*/; 


    public void testJSNI(){ 
     Window.alert("Working"); 
    } 

} 

它警告“无数据”,所以我知道我调用方法的方式有问题。它不能是一个静态的方法。

done回调里面,你将丢失this(它现在会指向其他东西)。

您需要通过bind或本地变量(var self = this)进行保存。

+0

另请参阅此答案:http://*.com/a/19915130/14955 – Thilo

+0

谢谢你的工作。 – james