Wicket: Component to the reference model from the panel

To illustrate my problem, let's say I have a Thing instance that has two text properties: "foo" and "bar".

I want to create a panel for editing 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() in my panel with an instance of IModel<Thing> and refer to this model for TextField components. What is the best way 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 TextField components by overriding the getObject() method to extract an object from the Panel Panel model?

None of them seem very elegant to me, so I was wondering if there is a better way?

+4
source share
3 answers

You can use PropertyModel for text fields. Pass the IModel<Thing> to the PropertyModel constructor with foo as the property name:

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

PropertyModel finds out that thingModel is a Model and calls getObject().getFoo() , etc.

This assumes that the IModel<Thing> instance does not change, only its base object, which can be changed, calls setDefaultModelObject .

+3
source

Maybe I just miss the point, but I can not find Panel.setModel() in JavaDocs neither 1.4 nor 1.5 . If this is something you have implemented, perhaps you could change it not to replace the model object, but to call model.setObject() instead?

Disclaimer: I can’t check right now, because there is no gate at work, and my home machine used to smash the video card ...

0
source

Maybe this will help?

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

A simple base class for IWrapModel objects.

See IComponentAssignedModel or IComponentInheritedModel, so you do not need to have empty methods like detach or setObject () when they are not used in the wrapper. The detach method causes the removal of wrapped patterns.

0
source

Source: https://habr.com/ru/post/1390465/


All Articles