Friday, June 1, 2012

Building a Wicket Component Which Updates Itself When the Model Changes

For a component to update itself when model is updated:
  • Implement a method (e.g. update) that updates the UI components. This can be called from:
    • the constructor and
    • whenever the model is changed.
  • Override the onModelChanged method, like this:
@Override
protected void onModelChanged() {
super.onModelChanged();
update();
}
  • onModelChanged will be automatically called by Wicket IF the reference of the Model Object has changed. Deep changes (e.g. changes in one of its fields) will not be detected.
  • So in order for onModelChanged be called, either:
    • replace the Model Object with a modified clone (in which case Wicket will automatically call it) OR
    • call onModelChanging & onModelChanged manually.
  • Ensure that the Component is added to an AjaxRequestTarget and it will be updated via AJAX automatically

No comments:

Post a Comment