2017-01-09 45 views
0

在我的GWT應用程序,我有第三方反應模塊 我需要從JavaScript到Java傳球成功/失敗回調就是我與數據通javascript的回調爲Java方法參數

JS PART

function() { 
     var reactCallback = { 
      success: function() { 
       console.log('success!'); 
      }, 
      error: function() { 
       console.log('error!'); 
      } 
     }; 
     window.reactRpcService(reactCallback); 
    } 
填充

JAVA GWT PART

public static native void reactRPCService(ReactRPCServiceAccessor reactRpc)/*-{ 
    $wnd.reactRpcService = function(callback){ 
     [email protected]::dispatchRPCRequest(???)(callback); 
    } 
}-*/; 


    private void dispatchRPCRequest(???) { 

    } 

計劃是調用java GWT RPC功能,數據標準化,並通過回調發送回javascript 我有想法通過它在Java中搜索爲JavascriptObject但不知道它是否是一個好方法

任何想法傢伙?

+0

您能指定您想要做的事嗎?應該轉移什麼?而Java端會發生什麼?僅僅調用Java函數,執行業務邏輯並根據結果調用不同的JS方法是不可能的? – mxlse

回答

0

其實我發現JavascriptObject根據的javadoc可以passed back into JSNI from Java becomes the original object, and can be accessed in JavaScript as expected.

所以在我的本地函數我加簽名這樣

public static native void setupNativeRactRPCServiceCalls(ReactRPCServiceAccessor reactRpc)/*-{ 
    $wnd.reactRpcService = function(callback){ 
     [email protected]::dispatchRPCRequest(Lcom/google/gwt/core/client/JavaScriptObject;)(callback); 
    } 
}-*/; 

private void dispatchRPCRequest(final JavaScriptObject callback) { 
...retrieve JSON data 
} 

,然後從服務器獲取數據後,我已經通過了回javascript

public static native void reactDataRedirector(JavaScriptObject callbackObject, String jsonData)/*-{ 
     callbackObject.success(jsonData); 
}-*/;