PDA

View Full Version : How do I use store api properly?



damir222
22 May 2008, 3:23 AM
Hi,

I have a TreeTable displaying data from TreeStore, and a FormPanel for editing this data.

I kind of like Record abstraction so I tried to use it to edit my data in a form.

I load the record from store into FormPanel and then i try to save it by calling record.endEdit(), which propagates the event to store and which should update my bound TreeTable.....

The problem is that store fires event like this

# fireStoreEvent(Update, RecordUpdate.COMMIT, record)

and TreeBinder tries to update the view from the same event, but from the model property which has not been initialized, and is null.

# protected void onUpdate(StoreEvent se) {
# update((M) se.model);
# }



So, is this a bug or I am not using the api properly?

damir222
23 May 2008, 2:11 AM
I have more questions/comments.

loading initial items into tree table also seems to have a bug:

# if (store.isFiltered()) {
# if (isOrDecendantSelected(null, element)) {
# item = createItem(element);
# }
# } else {
# item = createItem(element);
# }
# item = createItem(element);

item is created twice.

Also, tree table has items property which seems to hold all items, it is searched in update event for TableItem corresponding to the model instance which is updated, but items property is not filled from TreeTable.createAll or TreeTableBinder.renderChildren. Perhaps the created items should be inserted directly in createItem(), and it should probably use map<Model, Item> and not a List.

...

companioncabinet
23 May 2008, 5:54 AM
Morning...

You mentioned:


I kind of like Record abstraction so I tried to use it to edit my data in a form.

I load the record from store into FormPanel and then i try to save it by calling record.endEdit(), which propagates the event to store
How exactly do you load the record from store into your FormPanel? Do you have a code sample? I am wanting to do the same thing...

Thanks!

damir222
23 May 2008, 7:31 AM
Morning...

You mentioned:


I kind of like Record abstraction so I tried to use it to edit my data in a form.

I load the record from store into FormPanel and then i try to save it by calling record.endEdit(), which propagates the event to store
How exactly do you load the record from store into your FormPanel? Do you have a code sample? I am wanting to do the same thing...

Thanks!


I have for the time beeing created my own FormBinder where i register field and property name pairs that need to be copied in and out. I have also extended FormPanel with BoundFormPanel so i can add the field-property pair in a single line.

It is pretty basic implementation since it doesn't listen to the store or record events, I use it to save some typing in copying values in and out of form...
Currently i load values into form on SelectionChange event of a TreeTable, and save it back into data on button click.
I have gone back to using ModelData directly since i can't use the record functionality yet beacuse it is work in progress.

I don't know of BasicForm that has been mentioned, perhaps it is in svn.

I can post this simple implementation source if anybody could have use for it.

PS. question for darell, why doesn't record implement ModelData interface?

Damir

companioncabinet
23 May 2008, 9:07 AM
I'd like to see your form binder code. Thanks

damir222
24 May 2008, 2:54 AM
FormBinder



public class FormBinder {

HashMap<String, Field> fields;

public FormBinder() {
super();

fields = new HashMap<String, Field>();
}

public void register(String fieldName, Field field) {
fields.put(fieldName, field);
}

@SuppressWarnings("unchecked")
public void loadFromModelData(ModelData data) {
for (String fieldName : fields.keySet()) {
Field field = fields.get(fieldName);

Object value = data.get(fieldName);
field.setValue(value);
}
}

@SuppressWarnings("unchecked")
public void loadFromRecord(Record data) {
for (String fieldName : fields.keySet()) {
Field field = fields.get(fieldName);

Object value = data.get(fieldName);
field.setValue(value);
}
}

public void saveToModelData(ModelData data) {
for (String fieldName : fields.keySet()) {
Field field = fields.get(fieldName);
if (field.isReadOnly()) return;

data.set(fieldName, field.getValue());
}
}

public void saveToRecord(Record data) {
for (String fieldName : fields.keySet()) {
Field field = fields.get(fieldName);
if (field.isReadOnly()) return;

data.set(fieldName, field.getValue());
}
}

}



BoundFormPanel


public class BoundFormPanel extends FormPanel {

FormBinder binder;

public BoundFormPanel() {
super();
binder = new FormBinder();
}

public boolean addBound(String propertyName, Field field) {
binder.register(propertyName, field);
return super.add(field);
}

public void loadFromModelData(ModelData data) {
binder.loadFromModelData(data);
}

public void loadFromRecord(Record data) {
binder.loadFromRecord(data);
}

public void saveToModelData(ModelData data) {
binder.saveToModelData(data);
}

public void saveToRecord(Record data) {
binder.saveToRecord(data);
}
}



Usage:



// creating form

BoundFormPanel form = new BoundFormPanel();
TextField field = new TextField();
field.setFieldLabel("Name");
form.addBound("name", field);

// populating form (on eg. SelectionChange)
// data is instance of DataDbject that containts property "name"
form.loadFromModelData(data)

// saving form to model
form.saveToModelData(data)




Field properties are set with .setValue which goes through validation, if that is not desired .setValueRaw can be used.

Some fields currently blow up when setting null value from DataObject (LabelField, ChechBox...)

D.