wicket-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sebastien <seb...@gmail.com>
Subject Re: Centralizing ajax inline javascript
Date Sat, 29 Sep 2012 20:45:47 GMT
Hi Lucas,

Well, posting an AjaxCheckBox - for its onUpdate() to be fired - is a
slightly different use case than triggering a behavior with parameters as
we suggested...
The main difficulties resides in "how to post a component" (client side)
and "how to get the form-component back" (server side).

I will give you the key points as I would have done. There is probably
other (better?) possibilities.

1/ as we want to get an AjaxCheckBox back and call its onUpdate()... we
need to promote the visibility of onUpdate() because it is protected
2/ as the most efficient will be to create our class, we also remove the
inline click handler (that's the goal actually! :) ) and replace it by our
lighter one

abstract class MyAjaxCheckBox extends AjaxCheckBox
{
    public MyAjaxCheckBox(String id, IModel<Boolean> model)
    {
        super(id, model);

        // <2> remove click handler
        this.add(AttributeModifier.replace("onclick", "boup(this.id);"));
    }

    // <1> promotes visibility
    public abstract void onUpdate(AjaxRequestTarget target);
}


3/ Create the AbstractDefaultAjaxBehavior, that uses "wicketAjaxPost" to
post the element
4/ Get the component back on behavor's respond method:

public void renderHead(Component component, IHeaderResponse response)
{
    super.renderHead(component, response);

    response.renderJavaScript("function boup(id) { " +
this.getCallbackScript() + " }", "my-script-id");
}

protected CharSequence getCallbackScript()
{
    //<3> use wicketAjaxPost
    final CharSequence script = String.format("wicketAjaxPost('%s',
Wicket.Form.serializeElement(Wicket.$(id))", this.getCallbackUrl());
    return this.generateCallbackScript(script);
}

@Override
protected CharSequence getPreconditionScript()
{
    return "return Wicket.$(id) != null;"; //just to be consistent
}

@Override
protected void respond(AjaxRequestTarget target)
{
    IRequestParameters parameters =
RequestCycle.get().getRequest().getPostParameters();

    for (String name : parameters.getParameterNames())
    {
        // <4> Get the component back
        MyAjaxCheckBox checkBox = (MyAjaxCheckBox) MyPage.this.get(name);

        checkBox.processInput();
        checkBox.onUpdate(target);
    }
}

5/ attach the behavior to the checkboxes container


Hope this helps,
Sebastien.

On Sat, Sep 29, 2012 at 7:47 PM, lukuperman <lukuperman@hotmail.com> wrote:

> Thanks Ernesto and Sebastien! I'm going to apply your suggestions during
> the
> weekend and keep you posted. In the meantime, Ernesto, I want to clarify
> that I don't want to pass any additional parameter to the server. I just
> want the proper onUpdate() method of the proper AjaxCheckBox instance be
> called, for which I assume the URL you mention in step 2 can't be static
> but
> dynamically computed with the corresponding checkbox id (or name?) that I
> have clicked on. I hope it made sense since I'm not yet familiar with ajax
> callbacks URL concepts yet.
>
> Lucas Kuperman
>
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Centralizing-ajax-inline-javascript-tp4652499p4652533.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message