GWT RequestFactory和多个请求 [英] GWT RequestFactory and multiple requests
问题描述
有没有一种方法可以使用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();
但是我得到一个错误,请求已经在进行中。当我做了两个独立的EmployeeRequests:
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();
然后从浏览器发出两个不同的请求。我希望RequestFactory中的某些东西可以合并多个请求 - 我必须一次创建数百个实体,而且我不想提出数百个请求!
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!
推荐答案
是的,这是可能的。在你的第一个例子中,只需删除行
Yes, it's possible. In your first example, just remove the line
createReq.fire();
当您调用 createReq2.fire()
最后,GWT会在一个请求中发送newEmployee和newEmployee2(因为它们都保留在EmployeeRequest request >的上下文中)。我个人发现语义有点奇怪,但这只是我的看法。
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.
Riley的附录:
以下语法等同,更直观:
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();
这篇关于GWT RequestFactory和多个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!