PDA

View Full Version : Howto to properly load items into ComoBox?


Bugs
12-08-2008, 01:55 PM
I am trying to load a very simple Car domain object (id +name) into a ComboBox, but so far failing.

I have the following service:

@RemoteServiceRelativePath("exampleService")
public interface ExampleService extends RemoteService {
List<Car> getCars();
}
ExampleServiceAsync:

public interface ExampleServiceAsync {
void getCars(AsyncCallback<List<Car>> cars);
}
The ExampleServiceImpl read the Cars from the database.

I then try the following:

final ExampleServiceAsync service = Registry.get("exampleService");
final List<Car> cars = new ArrayList<Car>();

service.getCars(new AsyncCallback<List<Car>>() {

public void onFailure(Throwable caught) {
//
}

public void onSuccess(List<Car> result) {
cars.addAll(result);
}
});

ListStore<Car> store = new ListStore<Car>();
store.add(cars);

ComboBox<Car> combo = new ComboBox<Car>();
combo.setFieldLabel("Car names");
combo.setDisplayField("name");
combo.setStore(store);
The items are correctly retrieved from the database, but I am not so sure if this is the correct way to load the items.

:-?

zaccret
12-09-2008, 05:31 AM
You should fill the store on success :

final ListStore<Car> store = new ListStore<Car>();
service.getCars(new AsyncCallback<List<Car>>() {

public void onFailure(Throwable caught) {
//
}

public void onSuccess(List<Car> result) {
cars.addAll(result);
store.add(cars);
}
});

Bugs
12-09-2008, 07:11 AM
I ended up doing the following to load the items

final ExampleServiceAsync service = Registry.get("exampleService");

RpcProxy<Object, List<Car>> proxy = new RpcProxy<Object, List<Car>>() {
@Override
protected void load(Object loadConfig, AsyncCallback<List<Car>> callback) {
service.getCars(callback);
}
};

ListLoader<Car> listLoader = new BaseListLoader(proxy);
// Add loaded items to store
ListStore<Car> store = new ListStore<Car>(listLoader);

listLoader.load();

ComboBox<Car> combo = new ComboBox<Car>();
combo.setFieldLabel("Car");
combo.setDisplayField("name");
combo.setEditable(false);
combo.setStore(store);
eclipse complained about not defining the type for this line


ListLoader<Instance> listLoader = new BaseListLoader(proxy);
So I changed Instance to extend from BaseListLoadConfig instead of BaseModelData. Not sure if this make any difference though.

The BaseListLoader is a raw type and expects generics to be used. It expects a ListLoadResult. Not so sure how to use this, the only thing I found in the documentation is the following


LoadResult defines the object that is returned by the server in a data load operation. LoadResult objects can be of any type but many data loaders define a required type.


All this just to populate a dropdown...

:-/

zaccret
12-09-2008, 07:38 AM
Did you try my suggestion ? Maybe you can also use SimpleComboBox.

Bugs
12-09-2008, 08:22 AM
I did try it yes (and it works), but shouldn't the proper way of loading be done with a ListLoader (as my last example provided)?

I haven't looked into the SimpleComboBox yet, but I got the ComboBox working, so I don't see why to switch to it.

zaccret
12-09-2008, 08:43 AM
I did try it yes (and it works)

Ok, nice point.

but shouldn't the proper way of loading be done with a ListLoader

You may be right.

eclipse complained about not defining the type for this lineWell, actually, there are some minor (but annoying) issues with the usage of generics in the API, especially with the Store/Loader/Proxy/Reader stuff. I hope they will be addressed sooner :( Maybe you should post your code and point the issue in a new thread with a title near "Bad generics usage in ListLoader" in Bugs forum so that the Ext GWT team is aware of the problem.