Provide easier access to items of RefreshingView
------------------------------------------------

                 Key: WICKET-1227
                 URL: https://issues.apache.org/jira/browse/WICKET-1227
             Project: Wicket
          Issue Type: Improvement
          Components: wicket
            Reporter: Peter Ertl
         Attachments: AccessItemsOfRefreshingView.patch

When trying to directly access items of a RefreshingView (or subclasses of it) 
everything gets really complicated.

For Example:

When inserting a new, blank post into a guestbook the input focus should be set 
to the input field 'author'.

  public void onClick(final AjaxRequestTarget target)
  {
    // add new, blank entry to the guestbook posts model
    posts.add(new Post());

    // 
    // code that gets us the last item of the repeating view.
    // however, the view first has to be rendered in order to contain the new 
item
    // as it's only in the model but will not appear in the RefreshingView 
unless
    // RefreshingView#onBeforeRender() is called

    Item lastItem = ...???...

    target.addComponent(/* container that contains the refreshing view */);
    target.focusComponent(lastItem.get("author")); 
  }

Now what I did is attach a patch to provide methods for updating and getting 
the items of the RefreshingView.

the above example would look like this:

public void onClick(final AjaxRequestTarget target)
{
  posts.add(new Post());
  guestbookView.refresh();

  Item[] items = guestbookView.items();
  Item lastItem = items[items.length() - 1];

  target.addComponent(/* container that contains the refreshing view */);
  target.focusComponent(lastItem.get("author")); // put focus on "author" 
component where user can enter his name
}

Basically it works like this:

the refresh() method will invoke onBeforeRender() to populate the items of the 
view (based on the current model). That way you can access the items before the 
actual render occurs (using the iterator function MarkupContainer#getItems() ). 
Once the render occurs it would be just too late...

In order to prevent unnecessary subsequent calls of onBeforeRender() a flag 
will remember if the view needs refreshing, so onBeforeRender() is only called 
once per render (unless you call refresh() for another time).

items() is a convenience method that collects the current items using 
MarkupContainer#getItems() and returns a handy array of items and avoids 
cumbersome code.

I also added firstItem() and lastItem() as I think these will be quite common 
use cases for accessing that array.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to