Search code examples
gwtresty-gwt

Loading Resty-GWT


How can I show load widget, when client send json on server.

From example in GWTP exmaple I found such method

/**
* We display a short lock message whenever navigation is in progress.
*
* @param event The {@link LockInteractionEvent}.
*/
@ProxyEvent
public void onLockInteraction(LockInteractionEvent event) {
getView().setLoading(event.shouldLock());
}

How do I show in the loading resty-gwt, when it sent the request? Can I use onLockInteraction with resty-gwt?


Solution

  • You can use RestyGWT custom Dispatcher to track request lifecycle. Dispatcher can be configured manually or using annotations (https://resty-gwt.github.io/documentation/restygwt-user-guide.html). Example setting it manually:

    RootRestService rest = GWT.create(RootRestService.class);
    ((RestServiceProxy) rest).setDispatcher(new DefaultDispatcher() {
        @Override public Request send(Method m, RequestBuilder rb) throws RequestException {
            RequestCallback callback = rb.getCallback();
            rb.setCallback(new RequestCallback() {
                @Override public void onResponseReceived(Request req, Response res) {
                    log.info("request success (stop event)");
                    callback.onResponseReceived(req, res);
                }
                @Override public void onError(Request req, Throwable ex) {
                    log.info("request error (stop event)");
                    callback.onError(req, ex);
                }
            });
            try {
                log.info("request initialized (start event)");
                return request = super.send(m, rb);
            } finally {
                log.info("request fail to initialize error (stop event)");
            }
        }
    });
    

    Instead of logging, you can send an event using the eventBus, and use this event to keep track of the number of active request, and finally show a loading indicator if the number of active request is grater than 0.