GWT RequestFactory and multiple requests

跟風遠走 提交于 2019-12-03 10:03:53

问题


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!


回答1:


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();


来源:https://stackoverflow.com/questions/4961102/gwt-requestfactory-and-multiple-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!