Wicket and complex Ajax scenarios

2019-01-26 15:51发布

问题:

When a screen has multiple interacting Ajax controls and you want to control the visibility of components to react to these controls (so that you only display what makes sense in any given situation), calling target.addComponent() manually on everything you want to update is getting cumbersome and isn't very maintainable.

Eventually the web of onClick and onUpdate callbacks can reach a point where adding a new component to the screen is getting much harder than it's supposed to be.

What are the commonly used strategies (or even libraries if such a thing exists) to avoid this build-up of complexity?

Update: Thank you for your answers, I found all of them very useful, but I can only accept one. Sorry.

回答1:

Well, of how many components do we speak here? Ten? Twenty? Hundreds?

For up to twenty or about this you can have a state controller which controls which components should be shown. This controller sets the visible field of a components model and you do always add all components to your requests which are handled by the controller. The components ajax events you simply redirect to the controller handle method.

For really large numbers of components which have a too heavy payload for a good performance you could use javascript libraries like jQuery to do the show and hide things by the client.



回答2:

In Wicket 1.5 there is an event bus. Each component has onEvent(Object payload) method. With component.send() you can broadcast events and each component can check the payload (e.g. UserJoinedEvent object) and decide whether it wants to participate in the current Ajax response. See http://www.wicket-library.com/wicket-examples/events/ for a simple demo.



回答3:

You could add structural components such as WebMarkupContainers, when you add this to the AjaxTarget everything contained in it will also get updated. This allows you to update groups of components in a single line.



回答4:

When I'm creating components for a page I tend to add them to component arrays:

Component[] pageComponents = {
                  new TextField<String>("Field1"),
                  new TextField<String>("Field2"),
                  new TextField<String>("Field3")
}

As of Wicket 1.5 the add functions take array parameters [1]. Therefore elements can be added to the page or target like this:

add(pageComponents);
target.add(pageComponents);

Components can then be grouped based on which you want to refresh together.

[1] http://www.jarvana.com/jarvana/view/org/apache/wicket/wicket/1.5-M3/wicket-1.5-M3-javadoc.jar!/org/apache/wicket/ajax/AjaxRequestTarget.html



回答5:

I currently use some sort of modified Observer-Pattern to simulate an event-bus in Wicket 1.4.

My Pages act as an observable observer since my components don't know each other and are reused in different combinations across multiple pages. Whenever one component receives an Ajax-event that could affect other components as well, it calls a method on it's page with an event-object and the ajax-target. The page calls a similar method on all components which have registered themselves for this kind of event and each component can decide, on the base of the supplied event-object if and how it has to react and can attach itself to the target.

The same can be archived by using the wicket visitor. I don't know which one is better, but I think that's mainly a matter of taste.



标签: ajax wicket