Jeremy,
People will find your blog entry useful, but this issue is a bit more
specific.
AbstractPropertyModel _should_ implement IChainingModel, that's how you know
it _should_ dig values out model objects which r themselves models.
Now try this:
public class TestPropertyModelsChainingImpl extends WebPage {
Point p = new Point(3,4);
Model m = new Model(p);
class Wrapper{Wrapper(Point p){this.object = p;}Point object;}
Wrapper w = new Wrapper(p);
public TestPropertyModelsChainingImpl() {
add(new Label("x", new PropertyModel(this, "m.object.x")));
add(new Label("y", new PropertyModel(this, "w.object.y")));
}
}
<html><body>,</body></html>
That prints out 3,4 as expected. But my property expression has had to dig
into the nested model to get the object I'm after. What if my backing object
were more deeply nested?
In my opinion the firs line in the constructor should not need to worry
about the level of nestedness
but add(new Label("x", new PropertyModel(this, "x"))); will not work with
the current implementation.
Regards - Cemal
http://www.jWeekend.co.uk http://jWeekend.co.uk
Jeremy Thomerson-5 wrote:
>
> John, Cemal - that method in AbstractPropertyModel absolutely does abide
> by
> the contract - look at what it calls (getTarget()):
>
> public final Object getTarget()
> {
> Object object = target;
> while (object instanceof IModel)
> {
> Object tmp = ((IModel<?>)object).getObject();
> if (tmp == object)
> break;
> object = tmp;
> }
> return object;
> }
> John - I think you're right - it is unnecessary to implement
> IChainingModel
> since there is no code in the wicket core codebase that calls it's
> methods.
> Just make sure that if you nest models inside of other custom models, you
> do
> call detach properly or you can end up with a memory leak. You can see
> this
> blog post as a simple example:
> http://www.jeremythomerson.com/blog/2008/11/06/wicket-the-power-of-nested-models/
>
> Hope this helps.
>
> --
> Jeremy Thomerson
> http://www.wickettraining.com
>
>
> On Thu, Dec 4, 2008 at 9:31 AM, jWeekend
> <[EMAIL PROTECTED]>wrote:
>
>>
>> John,
>>
>> Well spotted. AbstractPropertyModel implements IChainingModel but breaks
>> its
>> contract :
>> * Models that implement this interface will support chaining of IModels.
>> getObject() of a
>> * IChainingModel should do something like:
>> *
>> * <pre>
>> * if ( object instanceof IModel) { return ((IModel)object).getObject()}
>> * else return object;
>> * </pre>
>>
>> Its getObject() implementation does not abide by this.
>> /**
>> * @see org.apache.wicket.model.IModel#getObject()
>> */
>> public Object getObject()
>> {
>> final String expression = propertyExpression();
>> if (Strings.isEmpty(expression))
>> {
>> // Return a meaningful value for an empty property
>> expression
>> return getTarget();
>> }
>>
>> final Object target = getTarget();
>> if (target != null)
>> {
>> return PropertyResolver.getValue(expression,
>> target);
>> }
>> return null;
>> }
>>
>> Regards - Cemal
>> http://www.jWeekend.co.uk <http://www.jweekend.co.uk/>
>> http://jWeekend.co.uk <http://jweekend.co.uk/>
>>
>>
>>
>> John Patterson wrote:
>> >
>> > Hi,
>> >
>> > I'm messing about with nested models and see the IChainingModel
>> interface
>> > which is implemented by the PropertyModels but its methods never
>> actually
>> > seems to be used.
>> >
>> > So I'm just checking that I haven't missed anything... for nested
>> models
>> I
>> > don't actually need to implement this right?
>> >
>> > This is its def:
>> >
>> >
>> > public interface IChainingModel<T> extends IModel<T>
>> > {
>> > /**
>> > * Sets the model that is chained inside this model.
>> > *
>> > * @param model
>> > */
>> > public void setChainedModel(IModel< ? > model);
>> >
>> > /**
>> > * Returns the chained model if there is a chained model.
>> > *
>> > * @return The chained model
>> > */
>> > public IModel< ? > getChainedModel();
>> >
>> > }
>> >
>> > Thanks,
>> >
>> > John
>> >
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/IChainingModel-tp20835085p20835745.html
>> Sent from the Wicket - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>
>
--
View this message in context:
http://www.nabble.com/IChainingModel-tp20835085p20851816.html
Sent from the Wicket - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]