views:

72

answers:

2

Hi!

Are there any techiques to collect a number of gwt-rpc service callback results?

I have a dialog window used to create new or edit existing object. These objects have a number of references to other object. So when user creating or editing an object he may pick one in the ListBox.

public class School {
    private String name;
    private String address;
}

public class Car {
    private String model;
    private String type;
}

public class Person {
    private String firstName;
    private String lastName;
    private School school;
    private Car firstCar;
}

When the dialog window appears on the screen it should request all available values for all referencing fields. These values are requested with AsyncCallback's via gwt-rpc, so I can handle it one-by-one.

service.getAllSchools(new AsyncCallback<List<School>>() {
    @Override
    public void onSuccess(List<School> result) {
        fillSchoolListBox(result);
    }

    @Override
    public void onFailure(Throwable caught) {
        Window.alert("ups...");
    }
});
...
service.getAllSchools(new AsyncCallback<List<Car>>() {
    @Override
    public void onSuccess(List<Car> result) {
        fillCarListBox(result);
    }

    @Override
    public void onFailure(Throwable caught) {
        Window.alert("ups...");
    }
});

How to get all result in one place? Thanks.

A: 

Why don't you create a new service method that returns all the data as a result?

The implementation of such a method could simply call all of the other methods. You will have to encapsulate all the required data and return it as a single result. One example how you could handle this:

In the service implementation:

@Override
public Data getAllData(){

    List<Cars> cars = this.getAllCars();
    List<School> schools = this.getAllSchools();

    return new Data(cars, schools);
}

And you can then use the method like this:

service.getAllData(new AsyncCallback<Data data>() {
    @Override
    public void onSuccess(Data data) {
        fillCarListBox(data.getCars());
        fillSchoolListBox(data.getSchools());
    }

    @Override
    public void onFailure(Throwable caught) {
        Window.alert("Pogreska...");
    }
});

With this kind of approach you minimize the number of service calls on your client side. This not only creates a more readable code, but also usually speeds up the client side of your app. You should always try to minimize the number of service calls, ideally to a single one.

Concerning the more general question of collecting a number of asynchronous callbacks, a good approach is to use the Command Pattern. Gwt Remote Action is a library that provides an implementation of the mentioned pattern for doing RPC calls:

http://code.google.com/p/gwt-remote-action/

igorbel
A: 
kospiotr