如何在客户端编辑gwt requestfactory的ValueProxy的值?

时间:2011-09-17 04:44:30

标签: gwt requestfactory

我有2个型号:ContactGroup和Contact。 ContactGroup包含许多联系人。

在页面中,我必须显示通讯组中的组列表和联系人数量,如下所示:

  • Group Foo(12位联系人)
  • Group Bar(20个联系人)

所以我在服务器端使用了DTO ContactGroupInfo:

public class ContactGroupInfo {
    private Integer contactCount;
    private Long id;
    private String name;

    public Integer getContactCount() { return this.contactCount; }
    public Long getId() { return this.id; }
    public String getName() { return this.name; }
    public void setContactCount(Integer count) { this.contactCount = count; }
    public void setId(Long id) { this.id = id; }
    public void setName(String name) { this.name = name; }
}

在此ContactGroupInfo中,我添加了 contactCount字段,该字段不是ContactGroup实体中的字段

在客户端,我使用了ValueProxy:

@ProxyFor(value = ContactGroupInfo.class, locator = ContactGroupService.class)
public interface LightContactGroupProxy extends ValueProxy {
    Integer getContactCount();
    Long getId();
    String getName();
    void setContactCount(Integer count);
    void setId(Long id);
    void setName(String name);
}

因此,当服务器端返回LightContactGroupProxy的客户端列表时,我将该列表存储在ArrayList中以呈现给CellTable。

这就是问题所在:当我需要在客户端编辑组的名称时,我无法直接编辑LightContactGroupProxy对象。

  • 所以我必须将新名称发送到服务器以返回带有新名称的新LightContactGroupProxy。这是无效的,因为我必须再次计算联系人(虽然我知道联系人的数量不会改变)。
  • 或者我必须向服务器发送联系人数量和新名称,以使用新名称创建新的LightContactGroupProxy。这不是我想要的,因为如果LightContactGroupProxy有许多其他字段,我必须发送许多字段。

我不知道为什么GWT团队会设计不可变代理。所以请有人对requestfactory有经验请告诉我处理从服务器返回的ValueProxy的正确方法,以便我们可以使用它们进行渲染和编辑?

谢谢

1 个答案:

答案 0 :(得分:1)

也许你应该尝试这样的事情:

ContactGroupContext ctx = requestFactory.newContactGroupContext();
LightContactGroupProxy editableProxy = ctx.edit(lightContactGroupProxy);
editableProxy.setName(newName);
ctx.saveInfoAndReturn(editableProxy).fire(receiver); // or just ctx.fire();

无论如何,我不会在这种情况下使用ValueProxy,我会直接获得具有transiant属性ContactGroup的{​​{1}}实体。如果您不希望每次请求contactCount时计算该属性,则该属性可以是原始属性,也可以是ValueProxy

相关问题