Skip to content

Instantly share code, notes, and snippets.

@slugmandrew
Last active December 23, 2015 12:59
Show Gist options
  • Save slugmandrew/6638630 to your computer and use it in GitHub Desktop.
Save slugmandrew/6638630 to your computer and use it in GitHub Desktop.
Creating a global RPC system for GWTP
public interface AbstractAsyncCallback<T> extends AsyncCallback<T>
{
// this method is not implemented in AsyncCallbackImpl, but in each presenter separately
void onReturn(T result);
}
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HasHandlers;
import com.google.web.bindery.event.shared.EventBus;
/**
* Global RPC System
*/
public abstract class AsyncCallbackImpl<T> implements AbstractAsyncCallback<T>, HasHandlers
{
@Inject
private static EventBus eventBus;
// Don't forget "requestStaticInjection(AsyncCallbackImpl.class)" in your Clientmodule or GIN won't inject this!
public AsyncCallbackImpl()
{
// perform any pre-dispatch logic here
fireEvent(new ActionSentEvent()); // adds loading bar
}
// perform any post-dispatch logic here
@Override
public void onSuccess(T result)
{
// do the success action (implemented in each presenter)
onReturn(result);
fireEvent(new ActionReturnedEvent()); // removes loading bar
}
@Override
public void onFailure(Throwable caught)
{
fireEvent(new ActionReturnedEvent()); // removes loading bar
Window.alert("Communication to the server has failed.");
MyUtilities.processThrowable(caught); // utility class deals with exception
}
@Override
public void fireEvent(GwtEvent<?> event)
{
eventBus.fireEvent(event);
}
}
private void addAccountDetails()
{
GetAccounts action = new GetAccounts();
dispatchAsync.execute(action, new AsyncCallbackImpl<GetAccountsResult>()
{
@Override
public void onReturn(GetAccountsResult result)
{
accounts = result.getAccounts();
processAccountDetails(accounts);
}
});
// method above is chained from onSuccess (which also executes global code to stop showing loading bar)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment