2011-03-15 81 views
0

我需要爲GWT中的onSuccess方法執行方法攔截。如何攔截GWT中的onSuccess()方法

我需要在GWT中的onSuccess方法的調用後和之前添加一些代碼? (我有很多來電來的onSuccess方法,我需要動態地做到這一點)

編輯: 我需要在屏幕的右上角添加一個progress bar,出現當代碼進入onsuccess方法消失在onsuccess方法的出口處。

+0

假設它是的onSuccess後一個非常大的過程(通常不應該是這種情況...)...你可以嘗試像BobVs的答案..你必須在onSuccess本身。但是 - 爲了澄清,你是否想在啓動rpc後立即顯示進度條,並在rpc完成時停止進度條?那麼它是一個差異化過程。讓我知道 – Jai 2011-03-15 14:37:20

回答

0

你不能這樣做。 rpc onSuccess()方法異步運行(換句話說,取決於服務器何時完成,應用程序不會等待它)。您可以在rpc調用之後立即觸發代碼,這可能會在RPC調用的onSuccess之前完成。

你能解釋一下,爲什麼你要這樣做?由於這種異步行爲,你可能不得不重新設計應用程序,但不能說直到你提供一個用例。最好在rpc調用之後忘記任何異步功能,並且僅在onSuccess中執行。

+0

我編輯了我的問題.. – 2011-03-15 12:09:04

1

從可視角度看

void onSuccess(Value v) { 
    showProgressBar(); 
    doLotsOfWork(v); 
    hideProgressBar(); 
} 

將是一個空操作。瀏覽器通常會等待事件處理程序在重新發布DOM之前完成執行。如果doLotsOfWork()方法需要大量時間執行(例如> 100ms),則由於JavaScript執行的單線程性質,用戶會注意到瀏覽器打嗝。

取而代之的是,考慮使用incrementally-scheduled command來打破工作。它看起來大致如下:

void onSuccess(Value v) { 
    showProgressBar(); 
    Scheduler.get().scheduleIncremental(new RepeatingCommand() { 
    int count = 0; 
    int size = v.getElements().size(); 
    public boolean execute() { 
     if (count == size) { 
     hideProgressBar(); 
     return false; 
     } 
     processOneElement(v.getElements().get(count++)); 
     setProgressBar((double) count/size); 
     return true; 
    } 
    }); 
} 

通過打破跨瀏覽器的事件循環的多個泵工作,避免了同時進行的web應用程序變得沒有響應,如果有做工作的非平凡量的情況。

1

嗯,這是一個通用的非功能性需求,我已經做了關於這個項目的一些研究,我已經實現,托馬斯Broyer曾建議對gwt group的解決方案。這個解決方案具有比其他建議的解決方案明顯的優勢,你不必須改變你的回調類,你所要做的僅僅是添加一行創建異步GWT-RPC服務後的代碼是什麼?

IGwtPersistenceEngineRPCAsync persistenceEngine = GWT.create(IGwtPersistenceEngineRPC.class); 
    ((ServiceDefTarget) persistenceEngine).setRpcRequestBuilder(new ProgressRequestBuilder()); 


import com.allen_sauer.gwt.log.client.Log; 
import com.google.gwt.http.client.Request; 
import com.google.gwt.http.client.RequestBuilder; 
import com.google.gwt.http.client.RequestCallback; 
import com.google.gwt.http.client.Response; 
import com.google.gwt.user.client.rpc.RpcRequestBuilder; 

public class ProgressRequestBuilder extends RpcRequestBuilder { 

    private class RequestCallbackWrapper implements RequestCallback { 

     private RequestCallback callback; 

     RequestCallbackWrapper(RequestCallback aCallback) { 
      this.callback = aCallback; 
     } 

     @Override 
     public void onResponseReceived(Request request, Response response) { 
      Log.debug("onResposenReceived is called"); 
      // put the code to hide your progress bar 
      callback.onResponseReceived(request, response); 

     } 

     @Override 
     public void onError(Request request, Throwable exception) { 
      Log.error("onError is called",new Exception(exception)); 
      // put the code to hide your progress bar 
      callback.onError(request, exception); 

     } 

    } 



    @Override 
    protected RequestBuilder doCreate(String serviceEntryPoint) { 

     RequestBuilder rb = super.doCreate(serviceEntryPoint); 
     // put the code to show your progress bar   
     return rb; 
    } 

    @Override 
    protected void doFinish(RequestBuilder rb) { 
     super.doFinish(rb); 
     rb.setCallback(new RequestCallbackWrapper(rb.getCallback())); 

    } 

}