Whenever I have to add some component (A) to the AjaxRequestTarget of another component (B) (usualy both children of the same parent, I face the same decision:
Make A a member-variable of the parent component vs. using the parents get(path) method. Both variants seem to have pros and cons and I can’t really decide which would be better from the perspective of “better” code…
The first variant is stable even if the path of (A) changes but leaves the parent cluttered with members that are only rarely used. The other variant results in a cleaner parent class but adds multiple points of failure when the component hierarchy changes. Additional this variant either litters the class with magic strings (the paths) or string constants (when I put the paths there)…
Any hints would be apreciated.
Edit: This applies to Wicket 1.4 since Wicket 1.5 solves this with its EventBus
Use Wicket 1.5’s event mechanism instead to update components using Ajax. This way you will decouple publisher and subscriber. See for example my presentation on new features in Wicket 1.5 given at JavaZone’11 (skip to about 51 minutes into the talk).
Taken from the Wicket 1.5 release notes:
Inter-component events
Wicket 1.5 offers a simple, yet flexible, way for component to communicate with each other in a decoupled manner. The two major interfaces that facilitate this are:
and
The classes that implement these interfaces, and can thus participate in the event mechanism are:
Component,RequestCycle,Session, andApplication.The mechanism allows for different event broadcast methods defined here:
There is an example in wicket-examples which demonstrates the usage of this.
Applications can register custom event dispatchers in FrameworkSettings; the dispatchers can be used to build custom event delivery mechanisms. For example a custom IEventDispatcher mechanism can route events to annotated methods, for example:
where
UserAddedEventis the event payload object.The default
Component#onEventmethod will be called even if custom dispatchers are registered.A default event is raised whenever Wicket begins to create an AJAX response. The payload of the event is the AjaxRequestTarget used for event. Sample implementation: