8

I have a AjaxPagingNavigator. Basically on a certain condition, the list which the AjaxPagingNavigator pages is reloaded. When this happens I only want to render the navigator when the list contains more than 1 page.

So does anyone know where I can attach a handler so that I can check for a visibility condition in my AjaxPagingNavigator and enable/disable visibility so that when the navigator is updated via. ajax it is either visible or not?

Markup:

<div wicket:id="mainWrap">
    <div wicket:id="navigator"/>
    <div wicket:id="listWrap">
        <div wicket:id="list><!-- here be content --></div>
    </div>
</div>

So I have an ajax event which refreshes "mainWrap" which refreshes the "navigator" along with the "list" and wrappings.

this is the event that triggers the whole thing.

 protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
     List foo = null; // do work to get list
     model.setFound(found); // update the model (thus updating "list")
     target.addComponent(mainWrap);
}

Edit: I know I can write

navigator.setVisibility(list.getPageCount() > 1);

after creating the navigator and after updating the model, but I was hoping to encapsulate that in a subclass of AjaxPagingNavigator.

Martijn Dashorst
  • 3,652
  • 17
  • 26
Dmitriy Likhten
  • 5,076
  • 7
  • 36
  • 47

2 Answers2

16

Be careful with doing expensive computations in an overridden isVisible method, as Wicket will call isVisible multiple times per request—not counting any calls you might inadvertently do.

Typically the best way to go about this is to override onConfigure and set the visibility flag manually.

@Override
void onConfigure() {
    super.onConfigure();
    setVisible(isVisible() && someExpensiveToCalculateCondition);
}

onConfigure is called just once during request processing, and called for all components, including those that are invisible (while onBeforeRender is only called for visible components).

Martijn Dashorst
  • 3,652
  • 17
  • 26
  • I very much like this answer. Thanks for resurrecting this issue so much in the future :) – Dmitriy Likhten Mar 19 '11 at 05:00
  • +1 - This one was a lifesaver (deserves two votes up :). But why calling "isVisible" on "onConfigure"? Using Dmitriy's example, if the first ajax returns "false" for "someExpensiveToCalculateCondition" and a second returns "true", I believe the "isVisible" will false the entire expression. – Eduardo Costa Aug 10 '11 at 16:28
  • Eduardo, this one obviously depends on the usecase. One usually calls isVisible() to respect visibility changes somewhere up the onConfigure chain. – msp Apr 12 '13 at 11:38
1

It's been a while since I touched Wicket, but if memory serves:

Can you not override the isVisible() method of your "navigator" object, such that it only displays under the condition you desire?

e.g. something like

.addComponent(new AjaxPagingNavigator(...) {
  @Override public boolean isVisible() { 
    return model.getFound().size() > 25;
  }
});
aw crud
  • 8,791
  • 19
  • 71
  • 115
  • Ha! I feel so stupid. Here I looking through all sorts of crazy behaviors to attach and set visibility. Oy! BTW "return super.isVisible() && this.getPageable().getPageCount() > 1;" is probably better since we want to ensure if someone else sets us to invisible we respect it. – Dmitriy Likhten Feb 05 '10 at 20:00
  • 2
    Don't feel stupid... when I was developing Wicket applications I usually spent a lot of time looking for solutions to my various problems, and when I found them they were almost always much simpler than I expected. – aw crud Feb 05 '10 at 20:27
  • 2
    I'll second that. I think the Wicket developers have done a great job, because usually as I look into an issue I realize how easy it was to solve/fix and more intuitive that I would have thought. – Matt Feb 08 '10 at 05:09
  • Should always setVisible in onConfigure(), for the reasons that are laid out in the answer. – Sarhanis Dec 01 '12 at 02:13
  • Indeed. As @Sarhanis mentions, overriding `isVisible` is [not recommended](http://wicketinaction.com/2011/11/implement-wicket-component-visibility-changes-properly/) by the Wicket gurus. Instead, override `onConfigure` and control visibility from there. – Stijn de Witt Oct 20 '14 at 10:18