开发者

GWT RequestFactory + CellTable

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 开发者_C百科I get an editable object - should I insert it back to table or forget about it?

Thanks for any help.


  • 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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜