Monday, June 7, 2010

The GWT RPC Service

In my previous post we created the initial MVP framework with a simple view to display our mock contacts. In this post we will add the RPC Service call to extract the mock contacts from our mock DAO, and list them on the screen.

The code to collect the contacts from our rpcService and displaying them on our ContactsView should be handled by the ContactsPresenter. After all, in the MVP framework, all logic to manipulate and present data should be in our presenters, never in our views.

You may also remember that doing RPC calls is expensive, and that you want to minimize the amount of data that gets transferred between server and client. That was why we created two types of contacts: Contact and ContactLight. Where the Contact class contains all our contact's data, the ContactLight class only contains an id and a display name. This is exactly all we need for showing the contacts in a list.

Revisiting The ContactsPresenter

So let's go ahead and tweak ContactsPresenter so that it has a private property to hold a List of ContactLight objects, and a new private method to fetch and display this list of ContactLight objects:
private List<ContactLight> contactsLight;

  /* Code left out for readability
  ...
  ...
  */

 private void fetchLightWeightContacts() {
  rpcService.getContactLightList(new AsyncCallback<List<ContactLight>>() {
   public void onSuccess(List<ContactLight> result) {
    contactsLight = result;
    List<String> data = new ArrayList<String>();
    for (int i = 0; i < result.size(); ++i) {
     data.add(result.get(i).getDisplayName());
    }
    display.setData(data);
   }
   public void onFailure(Throwable caught) {
    Window.alert("Error fetching contacts");
   }
  });
 }

As you can see, the rpcService.getContactLightList() method takes an AsynCallback object as parameter. This new ASyncCallback object has two methods: onSuccess and onFailure. The onSuccess method "returns" our desired List of ContactLight objects. The onFailure method "returns" a Thowable. Remember that this is a callback, so the rpcService actually takes our newly created AsyncCallback and calls the onSuccess method, passing in the retrieved list of ContactLight objects. In this onSuccess method, we added code that will parse the retrieved list, and pass it on in a format that the display will understand.

Now that the callback is ready, let's call this new private method fetchLightWeightContacts() from the go(HasWidgets container) method so that we populate the list the moment this display is painted on the screen:
@Override
 public void go(HasWidgets container) {
  container.clear();
  container.add(display.asWidget());
  fetchLightWeightContacts();
 }

All done. Go ahead and give the app a spin and let's check the results! You should now see the below:

We now have a nice screen showing all our contacts with their corresponding check box. Very nice, but our screen is currently not much use. None of the widgets (buttons, record rows, etc) have any events tied to it. Adding events to our widgets will be the next step.

No comments:

Post a Comment