GWT RequestFactory 和多个请求

发布于 2024-10-17 00:34:29 字数 1247 浏览 2 评论 0原文

有没有办法使用 RequestFactory 在单个请求中创建两个实体?我尝试过:

    EmployeeRequest request = requestFactory.employeeRequest();
    EmployeeProxy newEmployee = request.create(EmployeeProxy.class);
    newEmployee.setName("Joe!");

    Request<Void> createReq = request.persist().using(newEmployee);
    createReq.fire();

    EmployeeProxy newEmployee2 = request.create(EmployeeProxy.class);
    newEmployee2.setName("Sam!");

    Request<Void> createReq2 = request.persist().using(newEmployee2);
    createReq2.fire();

但我收到一个错误,表明请求已在进行中。当我发出两个单独的 EmployeeRequests 时:

    EmployeeRequest request = requestFactory.employeeRequest();
    EmployeeProxy newEmployee = request.create(EmployeeProxy.class);
    newEmployee.setName("Joe!");

    Request<Void> createReq = request.persist().using(newEmployee);
    createReq.fire();

    EmployeeRequest request2 = requestFactory.employeeRequest();
    EmployeeProxy newEmployee2 = request2.create(EmployeeProxy.class);
    newEmployee2.setName("Sam!");

    Request<Void> createReq2 = request2.persist().using(newEmployee2);
    createReq2.fire();

然后从浏览器发出两个单独的请求。我希望 RequestFactory 中的某些东西可以合并多个请求 - 我必须一次创建数百个实体,而且我不想发出数百个请求!

Is there a way to use RequestFactory to create two entities in a single request? I tried:

    EmployeeRequest request = requestFactory.employeeRequest();
    EmployeeProxy newEmployee = request.create(EmployeeProxy.class);
    newEmployee.setName("Joe!");

    Request<Void> createReq = request.persist().using(newEmployee);
    createReq.fire();

    EmployeeProxy newEmployee2 = request.create(EmployeeProxy.class);
    newEmployee2.setName("Sam!");

    Request<Void> createReq2 = request.persist().using(newEmployee2);
    createReq2.fire();

But I get an error that a request is already in progress. When I made two separate EmployeeRequests:

    EmployeeRequest request = requestFactory.employeeRequest();
    EmployeeProxy newEmployee = request.create(EmployeeProxy.class);
    newEmployee.setName("Joe!");

    Request<Void> createReq = request.persist().using(newEmployee);
    createReq.fire();

    EmployeeRequest request2 = requestFactory.employeeRequest();
    EmployeeProxy newEmployee2 = request2.create(EmployeeProxy.class);
    newEmployee2.setName("Sam!");

    Request<Void> createReq2 = request2.persist().using(newEmployee2);
    createReq2.fire();

Then two separate requests are made from the browser. I'm hoping that something in the RequestFactory can merge multiple requests - I have to create hundreds of entities at a time, and I don't want to make hundreds of requests!

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

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

发布评论

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

评论(1

記柔刀 2024-10-24 00:34:29

是的,这是可能的。在第一个示例中,只需删除最后一行

createReq.fire();

When you call createReq2.fire() ,然后 GWT 在一个请求中同时发送 newEmployee 和 newEmployee2 (因为它们都保留在您的上下文中) EmployeeRequest“请求”)。我个人觉得语义有点奇怪,但这只是我的意见。

赖利的附录:
以下语法是等效的并且更直观:

    EmployeeRequest request = requestFactory.employeeRequest();
    EmployeeProxy newEmployee = request.create(EmployeeProxy.class);
    newEmployee.setName("Joe!");

    request.persist().using(newEmployee);

    EmployeeProxy newEmployee2 = request.create(EmployeeProxy.class);
    newEmployee2.setName("Sam!");

    request.persist().using(newEmployee2);
    request.fire();

Yes, it's possible. In your first example, just remove the line

createReq.fire();

When you call createReq2.fire() at the end, then GWT sends both newEmployee and newEmployee2 in one single request (because they were both persisted in the context of your EmployeeRequest "request"). I personally find the semantics a bit strange, but that's just my opinion.

Addendum by Riley:
The following syntax is equivalent and is much more intuitive:

    EmployeeRequest request = requestFactory.employeeRequest();
    EmployeeProxy newEmployee = request.create(EmployeeProxy.class);
    newEmployee.setName("Joe!");

    request.persist().using(newEmployee);

    EmployeeProxy newEmployee2 = request.create(EmployeeProxy.class);
    newEmployee2.setName("Sam!");

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