Wicket - AjaxEventBehavior not rendered properly

207 views Asked by At

Look at the following snippet.

   add(new AjaxEventBehavior("onclick") {

        private boolean toggle = false;

        @Override
        protected void onEvent(AjaxRequestTarget target)
        {
            log.debug("onEvent: " + toggle);
            if (toggle)
            {
                toggle = false;
                target.prependJavaScript("toogle(true)");
            } 
            else
            {
                toggle = true;
                target.prependJavaScript("toogle(false)");
            }
         }        
     });

But after the page rendering [no errors, warnings], I could see no event associated to the DOM [verified by means of firebug]. Even the debug log was never printed.

Is there any option in wicket to verify the behavior is added or not?

3

There are 3 answers

1
mefi On

You should iterate trougth Behaviors added to this component to verify your one is added:

for (Behavior behavior : component.getBehaviors()) {
    if(AjaxEventBehavior.class.equals(behavior.getClass())) {
        // it works    
    }
}
1
martin-g On

The behavior won't contribute its JavaScript if the component it is attached on is either invisible or disabled.

BTW both of your prependJavaScript() calls use the same content toggle(true).

0
svenmeier On

You should (almost) never have HTML ids in your markup:

For Wicket these ids take precedence over generated unique markup ids. If the id is present multiple times on the page (e.g. if you use a component multiple times), all Ajax handlers will attach to the first markup tag with that id.