Hmm, this requires a bit low level Java understanding (Wicket doesn't
have anything to do with it.

The underlying mechanism is the same as doing this:

public void foo2bar(String a) {
    a = "bar";
}

String b = "foo";
System.out.println("b is now: " + b);
foo2bar(b);
System.out.println("b is now: " + b);

What do you expect here? [1]

Now consider this:

public class Foo2Bar {
    private String text;
    public Foo2Bar(String t) {
        this.text = t;
    }
    public void setText(String t) {
        this.text = t;
    }
    public toString() {
        return text;
    }
}

String b = "foo";
Foo2Bar f2b = new Foo2Bar(b);
System.out.println("f2b is now: " + f2b);
b = "bar";
System.out.println("f2b is now: " + f2b);

What do you expect now? [2]

Java copies references to objects. When you modify the original
reference to point to another object, the copy doesn't get notified of
this change. THis is what is happening in your property models: you
give it a reference to a person, which is copied (the reference). Next
you modify the reference, but don't provide a way to notify the
property model that it should point to another person.

So to fix the second example: we need to notify our f2b instance that
the reference has changed, by calling setText

Martijn

[1] foo and foo
[2] "f2b is now: foo" and "f2b is now: foo"
On Tue, Oct 28, 2008 at 10:35 PM, walnutmon <[EMAIL PROTECTED]> wrote:
>
> I did as you said, and have the unit tests, and read the wiki on chaining
> models... something still isn't clicking though.  BTW, I have Wicket in
> action and it is fantastic, perhaps there is still something elduing me
> though...  I still can't wrap my head around why that property model doesn't
> update in the first case you show me.  If the property model is calling that
> object, and doing a get description each time it's called it's own
> getObject() method, why doesn't changing the reference externally work?
>
> If you simply point me to a page number and tell me to read until I
> understand, I would be greatful!
>
> Thanks again!
> Justin
>
>
> Martijn Dashorst wrote:
>>
>> No defensive copying happening. Just your plain old references
>> updating. Read the models page on the wiki about chaining models.
>>
>> Put this in a unit test case:
>>
>> State s = new State();
>> s.setDescription("I haven't read Wicket in Action but hear it helps
>> solve these questions");
>> PropertyModel pm = new PropertyModel(s, "description");
>> assertEquals("I haven't read Wicket in Action but hear it helps solve
>> these questions", pm.getObject());
>>
>> s = new State();
>> s.setDescription("I'll buy Wicket in Action, just because I now get
>> why my property model doesn't know this new state yet.");
>> assertEquals("I'll buy Wicket in Action, just because I now get why my
>> property model doesn't know this new state yet.", pm.getObject());
>>
>> This is basically what you are doing in your panel.
>>
>> but if you did:
>> State s = new State("Foo");
>> Model m = new Model();
>> m.setObject(s);
>> PropertyModel pm = new PropertyModel(m, "description");
>> assertEquals("Foo", pm.getObject());
>>
>> and now for the coup de grace:
>>
>> s = new State("Bar");
>> m.setObject(s);
>> assertEquals("Bar", pm.getObject());
>>
>> Martijn
>>
>> On Fri, Oct 24, 2008 at 3:57 PM, walnutmon <[EMAIL PROTECTED]>
>> wrote:
>>>
>>> I have two panels, a view panel where you can look for news and an edit
>>> panel.  The edit panel has a reference to a "news" object and all of it's
>>> form elements have property models that use that object.
>>>
>>> When I pass a news object into the panel on creation all of the form
>>> elements fill as expected.  However, if I set that object through a
>>> setter
>>> in the panel class, the elements do not update.  My theory (which may be
>>> wrong) is that the property model makes a defensive copy and therefore is
>>> not linked to the object in the class.  If this is true, can I resend the
>>> object to the property model?
>>>
>>> If that's not true, any insight as to what I may be doing wrong?
>>> --
>>> View this message in context:
>>> http://www.nabble.com/Forcing-property-models-to-update-tp20150693p20150693.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]
>>>
>>>
>>
>>
>>
>> --
>> Become a Wicket expert, learn from the best: http://wicketinaction.com
>> Apache Wicket 1.3.4 is released
>> Get it now: http://www.apache.org/dyn/closer.cgi/wicket/1.3.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>>
>
> --
> View this message in context: 
> http://www.nabble.com/Forcing-property-models-to-update-tp20150693p20216529.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]
>
>



-- 
Become a Wicket expert, learn from the best: http://wicketinaction.com
Apache Wicket 1.3.4 is released
Get it now: http://www.apache.org/dyn/closer.cgi/wicket/1.3.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to