On Tue, 11 Mar 2008, tsuresh wrote:
> > When I click on the next name the output is User selected: Bob. i.e. The
> > first output comes in second click and second ouput comes in third click
> > and so on. My code is as shown below: Where am I missing ? Please help.
> > 
> >  String username ="";
> >   Link userLink = new Link("user") {
> > 
> >                     public void onClick() {
> >                         User selectedUser = (User)
> > getParent().getModelObject();
> >                         username = selectedUser.getUsername();
> >                        
> >                     }
> >                 };
> > 
> >                 userLink.add(new SimpleAttributeModifier("onclick",
> > "return alert('User selected:" +username + "');"));

Think about when the last line is evaluated: it happens 
already when the link is being constructed and the behavior 
is being appended onto it, so it uses the value that username 
has at that moment. Link.onClick() happens later and doesn't
change the existing AttributeModifier anymore.

Rather than that, you should do something more dynamic like

  userLink.add(new AttributeModifier("onclick", true, 
    new AbstractReadOnlyModel() {
        @Override
        public Object getObject() {
            return nameOfSelectedUser();
        }
    });

    ...

    private String nameOfSelectedUser() {
        return ((User) getModelObject()).getUserName();
    }


Best wishes,
Timo

-- 
Timo Rantalaiho           
Reaktor Innovations Oy    <URL: http://www.ri.fi/ >

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

Reply via email to