GWT 请求工厂 +单元表

发布于 2024-12-09 08:38:12 字数 2737 浏览 0 评论 0 原文

有谁知道 GWT 的 CellTable 使用 RequestFactory 的示例并且该表正在编辑?我想在表中列出对象(每一行是一个对象,每一列是一个属性),能够轻松添加新对象并进行编辑。我知道 Google 的 DynaTableRf 示例,但该示例无法编辑。

我搜索了 Google 和 stackoverflow,但没有找到。我对 RF 的上下文有点困惑,而且人们还提到了一些“驱动程序”。

为了演示我当前到达的位置,我附加了一列的代码:

        // Create name column.
    Column<PersonProxy, String> nameColumn = new Column<PersonProxy, String>(
            new EditTextCell()) {
        @Override
        public String getValue(PersonProxy person) {
            String ret = person.getName();
            return ret != null ? ret : "";
        }
    };


    nameColumn.setFieldUpdater(new FieldUpdater<PersonProxy, String>() {            
        @Override
        public void update(int index, PersonProxy object, String value) {
            PersonRequest req = FaceOrgFactory.getInstance().requestFactory().personRequest();
            PersonProxy eObject = req.edit(object);             
            eObject.setName(value);
            req.persist().using(eObject).fire();
        }
    });

以及数据提供程序的代码:

        AsyncDataProvider<PersonProxy> personDataProvider = new AsyncDataProvider<PersonProxy>() {
        @Override
        protected void onRangeChanged(HasData<PersonProxy> display) {
          final Range range = display.getVisibleRange();

          fetch(range.getStart());
        }
      };
    personDataProvider.addDataDisplay(personTable);

...

private void fetch(final int start) {
    lastFetch = start;
    requestFactory.personRequest().getPeople(start, numRows).fire(new Receiver<List<PersonProxy>>() {
        @Override
        public void onSuccess(List<PersonProxy> response) {
            if (lastFetch != start){
                return;
            }               
            int responses = response.size();

            if (start >= (personTable.getRowCount()-numRows)){                  
                PersonProxy newP = requestFactory.personRequest().create(PersonProxy.class);
                response.add(newP);
                responses++;
            }

            personTable.setRowData(start, response);

            personPager.setPageStart(start);
        }
    });     
    requestFactory.personRequest().countPersons().fire(new Receiver<Integer>() {
        @Override
        public void onSuccess(Integer response) {
            personTable.setRowCount(response+1, true);
        }
    });
}

我尝试将最后一个对象插入一个新的空对象。当用户填写它时,我会在其后插入新的。但代码不起作用。我说用户正在“尝试”编辑先前由另一个 RequestContext 编辑的对象。

困境:
* 我是否创建了太多上下文?
* 如何正确地将新对象插入到客户端创建的单元格表中?
* 在 fieldUpdater 上,当我获得可编辑对象时 - 我应该将其插入表中还是忘记它?

感谢您的帮助。

Does anyone know for an example of GWT's CellTable using RequestFactory and that table is being edited? I would like to list objects in a table (each row is one object and each column is one property), be able to easily add new objects and edit. I know for Google's DynaTableRf example, but that one doesn't edit.

I searched Google and stackoverflow but wasn't able to find one. I got a bit confused with RF's context and than people also mentioned some "driver".

To demonstrate where I currently arrived, I attach code for one column:

        // Create name column.
    Column<PersonProxy, String> nameColumn = new Column<PersonProxy, String>(
            new EditTextCell()) {
        @Override
        public String getValue(PersonProxy person) {
            String ret = person.getName();
            return ret != null ? ret : "";
        }
    };


    nameColumn.setFieldUpdater(new FieldUpdater<PersonProxy, String>() {            
        @Override
        public void update(int index, PersonProxy object, String value) {
            PersonRequest req = FaceOrgFactory.getInstance().requestFactory().personRequest();
            PersonProxy eObject = req.edit(object);             
            eObject.setName(value);
            req.persist().using(eObject).fire();
        }
    });

and my code for data provider:

        AsyncDataProvider<PersonProxy> personDataProvider = new AsyncDataProvider<PersonProxy>() {
        @Override
        protected void onRangeChanged(HasData<PersonProxy> display) {
          final Range range = display.getVisibleRange();

          fetch(range.getStart());
        }
      };
    personDataProvider.addDataDisplay(personTable);

...

private void fetch(final int start) {
    lastFetch = start;
    requestFactory.personRequest().getPeople(start, numRows).fire(new Receiver<List<PersonProxy>>() {
        @Override
        public void onSuccess(List<PersonProxy> response) {
            if (lastFetch != start){
                return;
            }               
            int responses = response.size();

            if (start >= (personTable.getRowCount()-numRows)){                  
                PersonProxy newP = requestFactory.personRequest().create(PersonProxy.class);
                response.add(newP);
                responses++;
            }

            personTable.setRowData(start, response);

            personPager.setPageStart(start);
        }
    });     
    requestFactory.personRequest().countPersons().fire(new Receiver<Integer>() {
        @Override
        public void onSuccess(Integer response) {
            personTable.setRowCount(response+1, true);
        }
    });
}

I try to insert last object a new empty object. And when user would fill it, I'd insert new one after it. But the code is not working. I says that user is "attempting" to edit a object previously edited by another RequestContext.

Dilemmas:
* am I creating too many context'es?
* how to properly insert new object into celltable, created on the client side?
* on fieldUpdater when I get an editable object - should I insert it back to table or forget about it?

Thanks for any help.

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

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

发布评论

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

评论(1

等风来 2024-12-16 08:38:12
  • 我是否创建了太多上下文?

是的。

每个 HTTP 请求(每个 fire())都应该有一个上下文,并且不是 fire()d 的上下文是无用的(只有在您/用户时才这样做)改变你/他的想法并且不想,例如,保存你/他的更改)。
实际上,您只有一个上下文需要删除(见下文)。

请注意,保存每个字段更改的方法可能会导致“竞争条件”,因为代理一次最多可以由一个上下文进行 edit() 编辑,并且它仍然附加到一个上下文直到服务器响应(并且一旦触发上下文,代理就会被冻结 - 只读 - 也直到服务器响应为止)。
(并非在所有情况下都是如此:调用 onConstraintViolation 时,上下文及其代理被解冻,因此您可以“修复”约束违规并再次触发上下文;这应该是安全的,因为验证是在服务器端完成的调用任何服务方法)。

  • 如何正确地将新对象插入到客户端创建的单元格表中?

您的代码看起来不错,只是您应该在与用于持久化它的上下文相同的上下文中创建代理。

  • 当我获得可编辑对象时,在 fieldUpdater 上 - 我应该将其插入表中还是忘记它?

我不是 100% 确定,但我认为你应该刷新表格(类似于 setRowData(index, Collections.singletonList(object)) )

顺便说一句,驱动程序人们提到可能是 RequestFactoryEditorDriver rel="nofollow">编辑器框架。它在这里对你没有帮助(实际上恰恰相反)。

  • am I creating too many context'es?

Yes.

You should have one context per HTTP request (per fire()), and a context that is not fire()d is useless (only do that if you/the user change your/his mind and don't want to, e.g., save your/his changes).
You actually have only one context to remove here (see below).

Note that your approach of saving on each field change can lead to "race conditions", because a proxy can be edit()ed by at most one context at a time, and it remains attached to a context until the server responds (and once a context is fired, the proxy is frozen –read-only– also until the server responds).
(this is not true in all cases: when onConstraintViolation is called, the context and its proxies are unfrozen so you can "fix" the constraint violations and fire the context again; this should be safe because validation is done on the server-side before any service method is called).

  • how to properly insert new object into celltable, created on the client side?

Your code looks OK, except that you should create your proxy in the same context as the one you'll use to persist it.

  • on fieldUpdater when I get an editable object - should I insert it back to table or forget about it?

I'm not 100% certain but I think you should refresh the table (something like setRowData(index, Collections.singletonList(object)))

BTW, the driver people mention is probably the RequestFactoryEditorDriver from the Editor framework. It won't help you here (quite the contrary actually).

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