Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Wicket) Change visibility during ajax response

Tags:

java

wicket

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.

like image 963
Dmitriy Likhten Avatar asked Feb 05 '10 19:02

Dmitriy Likhten


2 Answers

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).

like image 56
Martijn Dashorst Avatar answered Nov 08 '22 03:11

Martijn Dashorst


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;
  }
});
like image 29
aw crud Avatar answered Nov 08 '22 05:11

aw crud