Wicket and complex Ajax scenarios

后端 未结 5 1100
抹茶落季
抹茶落季 2020-12-19 16:37

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 sen

相关标签:
5条回答
  • 2020-12-19 16:48

    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.

    0 讨论(0)
  • 2020-12-19 16:50

    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.

    0 讨论(0)
  • 2020-12-19 16:53

    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.

    0 讨论(0)
  • 2020-12-19 16:58

    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

    0 讨论(0)
  • 2020-12-19 17:03

    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.

    0 讨论(0)
提交回复
热议问题