mnish
mnish

Reputation: 3897

wicket checkbox AttributeAppender

In a code that I am working on there is a normal(no ajax) checkbox to which I want to append the JavaScript onchange event to it like:

checkbox.add(new AttributeAppender("onchange", Model.of("func(this);"), ""));

There is also anAjaxEventBehavior("onclick") added to the checkbox.

The function that must be called by onchange event is called just once even I check and uncheck the checkbox multiple times. I am guessing this has to do with the 'ajax' 'onclick' event.

How to make the func(this) called whenever the checkbox checked or unchecked?

Thank you

Upvotes: 1

Views: 3632

Answers (2)

Cedric Gatay
Cedric Gatay

Reputation: 1583

Regarding my comments on the other answer, here is what I suggest using an AjaxCallDecorator

new AjaxEventBehavior('onclick'){
    protected void onEvent(AjaxRequestTarget target) {
      //call your onclick handling logic here
    }

    protected IAjaxCallDecorator getAjaxCallDecorator(){
      return new IAjaxCallDecorator(){
         public CharSequence decorateScript(Component component,
                        CharSequence script){
            //prepend your javascript call to ajax server call
            return "func(this);"+script;
         }
         public CharSequence decorateOnSuccessScript(Component component,
                                 CharSequence script){
            return script;
         }
         public CharSequence decorateOnFailureScript(Component component,
                                 CharSequence script){
            return script;
         }
      };
    }
}

Upvotes: 2

jordeu
jordeu

Reputation: 6821

May be you can call your func(this); function as a AjaxEventBehavior. getPreconditionScript() or getSuccessScript(), instead of adding and AttributeAppender on the onchange event.

Take a look at http://wicket.apache.org/apidocs/1.5/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.html#getPreconditionScript()

Your AjaxEventBehavior will look similar to this:

new AjaxEventBehavior("onclick") {

     protected void onEvent(AjaxRequestTarget target) {
             System.out.println("ajax here!");
     }

     protected CharSequence getSuccessScript() {
             return "func(this);";
     }
}

Upvotes: 2

Related Questions