Nicolai Erbs
Nicolai Erbs

Reputation: 23

Same AjaxEventBehavior for multiple components

I add an image and a text component to a WebMarkupContainer as described here:

filter.add(newFilterLabel("textSub", customerText, filtervalue));
filter.add(newFilterImage("imgSub", filtervalue));

For each component, there is an AjaxEventBehavior doing different things. I would like to change it in a way that both do the same thing independent from which component has been clicked on.

private Component newFilterLabel(String id, IModel<String> customText,
    final SourceFilterValue currentValue) {
    final BBLabel label = new BBLabel(id, customText);
    label.add(new AjaxEventBehavior("onclick") {
        private static final long serialVersionUID = 1L;

        @Override
        protected void onEvent(AjaxRequestTarget target) {
            doSomething(currentValue,filteredsources, target);
        }
    });
    return label;
}

private Image newFilterImage(String id, final SourceFilterValue filterValue) {
    final Image img = new Image(id, resources.getImage(EXPAND_ICON));
    img.add(new AjaxEventBehavior("onclick") {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onEvent(AjaxRequestTarget target) {
            doSomething(img);
        }
    });
    return img;
}

Do you have any suggestions how to change it or any workaround? I use Wicket 1.5.8.

Upvotes: 1

Views: 212

Answers (1)

svenmeier
svenmeier

Reputation: 5681

An AjaxBehavior can be bound to a single element only. Either add it to a parent in the hierarchy, or just let both behaviors call the same method:

@Override
protected void onEvent(AjaxRequestTarget target) {
    doSomething(filterValue); //filterValue has to be final to be able to access it from the inner class
}

Upvotes: 1

Related Questions