有没有办法使用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中的某些内容可以合并多个请求 - 我必须一次创建数百个实体,而且我不想发出数百个请求!
答案 0 :(得分:9)
是的,这是可能的。在第一个示例中,只需删除行
即可createReq.fire();
当你在最后调用createReq2.fire()
时,GWT会在一个请求中发送newEmployee和newEmployee2(因为它们都持久存储在EmployeeRequest“request
”的上下文中)。我个人觉得语义有点奇怪,但这只是我的观点。
莱利的补遗: 以下语法是等效的,更直观:
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();