mico
mico

Reputation: 12738

How to combine two List PropertyModels to one model in Wicket

Let's say I have an entity called E.

Its structure has two fields:

E {
   List<A> a1;
   List<A> a2;
}

Currently I have two PropertyModels like this:

PropertyModel<A> a1Model = new PropertyModel(e, "a1");
PropertyModel<A> a2Model = new PropertyModel(e, "a2");

I have one repeater, and I want to give it one listModel only.

My current way to achieve this is to make like this:

List<A> aItems = new ArrayList<A>();
aItems.addAll(a1Model.getObject());
aItems.addAll(a2Model.getObject());
PropertyModel<A> aModel = new PropertyModel<A>(this, "aItems");

I now miss connection to database, since e is actually given inside a LoadableDetachableModel. When e changes inside that model, the value does not automatically flow until the field.

I am not sure does it make it now either, but somehow interfering the play with models only smells bad to my nose. Now I have only list a1 in field, and now I am adding a2.

How to achieve one model for the field (it is actually another panel)?

Upvotes: 1

Views: 332

Answers (1)

Imperative
Imperative

Reputation: 3183

You might create a Model that combines both models to List<E>:

IModel<List<E>> listModel = new LoadableDetachableModel<List<E>>() {

    public List<E> load() {
        return Arrays.asList(a1Model.getObject(), a2Model.getObject());
    }

}

Upvotes: 3

Related Questions