简体   繁体   中英

Wicket: Component to reference model from containing Panel

To illustrate my problem, let's say I have an instance of Thing which has two text properties - 'foo' and 'bar'.

I want to create a Panel to edit instances of Thing. The panel has two TextField components, one for the 'foo' property and one for the 'bar' property.

I want to be able to call setDefaultModel() on my Panel with an instance of IModel<Thing> and for the TextField components to reference this model. How best to achieve this?

Should I override the Panel.setDefaultModel() method to also call setModel() on the two TextField components? Or perhaps create anonymous ReadOnlyModels for the TextField components, overriding the getObject() method to retrieve the object from the containing Panel's model?

Neither of these seem very elegant to me, so I was wondering if there's a better way?

You can use a PropertyModel for the textFields. Pass the IModel<Thing> into the constructor of the PropertyModel with foo as the property name:

add(new TextField("fooFieldId", new PropertyModel(thingModel, "foo")));

The PropertyModel will figure out that the thingModel is a Model and call getObject().getFoo() etc.

This assumes the IModel<Thing> instance doesn't change, only its underlying object which can be changed calling setDefaultModelObject .

Maybe this would help?

public abstract class AbstractWrapModel<T> extends Object implements IWrapModel<T>

Simple base class for IWrapModel objects.

See IComponentAssignedModel or IComponentInheritedModel so that you don't have to have empty methods like detach or setObject() when not used in the wrapper. The detach method calls the wrapped models detach.

Maybe I'm just missing the point, but I can't find a Panel.setModel() in the JavaDocs of neither 1.4 nor 1.5 . If it's something you implemented maybe you could change it not to replace the model object but to call model.setObject() instead?

Disclaimer : Can't really check right now, cause there is no wicket at work and my home machine suffered a video card breakdown earlier...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM