Hi Willis

The quickest solution to this one I've found is to use a normal PropertyModel, which doesn't seem to drill down through the nested models.

In your case

public class BigPage {
        public BigPage(BigObject object) {
                setModel(new CompoundPropertyModel(object))
add(new SmallComponent("smallObject", new PropertyModel(getModel(), "smallObject")));
        }
}

It seems to be a bit of a hack, but it works well enough. It prevents having to probe the BigObject by calling object.getSmallObject() yourself. It would be useful for the situation where you have a LoadableDetachableModel and don't want to instantiate it twice or load it in the constructor; you would just be able to wrap them like above.

Anyone have a cleaner solution - I come across this a lot, particularly when using panels. Is this something begging for a better abstraction?

Regards
Chris

On 16/02/2009, at 3:19 AM, Willis Blackburn wrote:

Hello,

I have a situation that keeps coming up. All of my solutions have seemed clumsy, which makes me think that there's a better way of approaching this that I just haven't figured out. Can someone point me in the right direction?

What I want is to have a Page that uses CompoundPropertyModel, and then include a component on that page that also uses CompoundPropertyModel. So roughly it looks like this:

public class BigObject {
   public SmallObject get SmallObject() { ... }
}

public class SmallObject {
   public String getName() { ... }
}

public class BigPage {
   public BigPage(BigObject object) {
       setModel(new CompoundPropertyModel(object));
       add(new SmallComponent("smallObject"));
   }
}

public class SmallComponent {
   public SmallComponent() {
       add(new Label("name"));
   }
}

If I try to do just this, then I get an error because the label that's part of SmallComponent finds the BigPage model and fails because there's no property of BigObject called name.

So obviously SmallComponent needs some model:

public class SmallComponent {
   public SmallComponent(IModel model) {
       setModel(new CompoundPropertyModel(model));
       add(new Label("name"));
   }
}

But what model to give it? I tried passing it new ComponentPropertyModel("smallObject"), which didn't work because ComponentPropertyModel implements IComponentAssignedModel and thus can't be directly wrapped in CompoundPropertyModel. Adding a call to wrap() in the SmallComponent constructor fixed the problem, but I'm not sure if I can just call wrap and carry on or if there will be some unforeseen consequence of that down the road. Is there a standard way of doing this?

Thanks,
Willis


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


--------
Christopher Armstrong
[email protected]






---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to