Wicket and complex Ajax scenarios

纵饮孤独 提交于 2019-11-29 11:10:25

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.

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.

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.

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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!