如何收集多个异步回调?

发布于 2024-09-10 10:30:50 字数 1129 浏览 12 评论 0原文

有没有什么技术可以收集一些gwt-rpc服务回调结果?

我有一个用于创建新对象或编辑现有对象的对话框窗口。这些对象有许多对其他对象的引用。因此,当用户创建或编辑对象时,他可以在列表框中选择一个对象。

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;
}

当对话框窗口出现在屏幕上时,它应该请求所有引用字段的所有可用值。这些值是通过 gwt-rpc 使用 AsyncCallback 请求的,因此我可以一一处理。

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...");
    }
});

如何将所有结果集中到一个地方? 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

擦肩而过的背影 2024-09-17 10:30:50

正如 igorbel 所说,最好的解决方案是 Command Patter,但如果您是初学者,您可以设计例如仅包含必须在一个请求时传输的 Bean 的 Bean 容器。

例如:
<代码>

public class BeanContainer{
    private ArrayList<School> schools = new ArrayList<School>();
    private ArrayList<Car> cars = new ArrayList<Car>;
    private ArrayList<Person> people = ArrayList<Person>();

public void addSchool(School school){
    this.schools.add(school);
}

public void addSchoolCollection(ArrayList<School> schools){
    this.schools.add(schools);
}

public ArrayList<School> getSchoolCollection(){
    return schools;
}

...

}

The best solution would be Command Patter as igorbel said, but if you are beginner you can design for example Bean Container that only contains beans that must be transported at one request.

For example:

public class BeanContainer{
    private ArrayList<School> schools = new ArrayList<School>();
    private ArrayList<Car> cars = new ArrayList<Car>;
    private ArrayList<Person> people = ArrayList<Person>();

public void addSchool(School school){
    this.schools.add(school);
}

public void addSchoolCollection(ArrayList<School> schools){
    this.schools.add(schools);
}

public ArrayList<School> getSchoolCollection(){
    return schools;
}

...

}

挖鼻大婶 2024-09-17 10:30:50

为什么不创建一个新的服务方法来返回所有数据作为结果?

这种方法的实现可以简单地调用所有其他方法。您必须封装所有必需的数据并将其作为单个结果返回。如何处理此问题的一个示例:

在服务实现中:

@Override
public Data getAllData(){

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

    return new Data(cars, schools);
}

然后您可以使用如下方法:

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...");
    }
});

通过这种方法,您可以最大限度地减少客户端的服务调用次数。这不仅会创建更具可读性的代码,而且通常还会加快应用程序的客户端速度。您应该始终尽量减少服务呼叫的数量,最好是一次。

关于收集大量异步回调的更普遍的问题,一个好的方法是使用命令模式。 Gwt Remote Action 是一个库,它提供了执行 RPC 调用的上述模式的实现:

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

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文