millimoose
millimoose

Reputation: 39950

How to make a wicket component that's re-rendered on every AJAX request to a page?

Is there a way to make a Wicket component re-render itself on every AJAX request to a page? (Essentially to do what ajaxRendered="true" does in RichFaces.)

My application basically consists of the following:

The save and undo buttons should be enabled or disabled depending on if there are any recorded changes or not. Since there's several ways to input changes (editing, importing a CSV, etc.), I want to avoid having to intercept every action that might change the saved state.

Also, the buttons are only shown on some pages, so I don't want to have to sniff them in the page in a custom WebRequestCycle.

Is there a hook that Wicket calls when an AJAX request is about to be processed, that's called for every component on the page? I know there's Component#onBeforeRender() and Component#onConfigure(), but the documentation for them doesn't state when they're called for AJAX requests.

Upvotes: 0

Views: 4739

Answers (2)

martin-g
martin-g

Reputation: 17503

In Wicket 1.5 there is an event bus. For each Ajax request an event is sent to each component and the component can use AjaxRequestTarget to add itself for re-render.

E.g.:

class MyComponent ... {

 public void onEvent(Object payload) {

   if (payload instanceod AjaxRequestTarget) {
     ((AjaxRequestTarget) payload).add(this);
   }
 } 

Upvotes: 9

Nicktar
Nicktar

Reputation: 5575

I was having a similar problem and thought of two possible solutions from which I used the second one.

  1. Call a method on your page in the Ajax-Handler, which in turn invokes a visitor to find all childs that need to be rerendered, adding them to the target.

  2. A variation of the observer-pattern... I had my components register themselves as wantToUpdate at the page and triggered a function, adding them all to the Requesttarget in my Ajax-Handler. I couldn't use the observer pattern out of the box since my components don't know each other and I didn't want to introduce this kind of coupling.

Upvotes: 2

Related Questions