Hi,

I'm implementing a control which consists of an integer field and a
slider where the slider comes from jquery UI.

My question is that whenever the component is refreshed via ajax, you
need to call the js initialization function.

I've done this by adding a behaviour which determines the request
target, if it's an AjaxRequestTarget it adds the init js to the target
otherwise it outputs it in the header.

Is this the correct way to do it?  Is there a simpler way?

Here is the code that I've implemented as of today

public class NumberFieldWithSlider<Number> extends FormComponentPanel<Number> {
    private TextField<Number> field;
    private String slider_id;

    public NumberFieldWithSlider(String id) {
        super(id);
        init();
    }

    public NumberFieldWithSlider(String id, IModel<Number> model) {
        super(id, model);
        init();
    }

    private void init() {
        field = new TextField("field", getModel());
        add(field);

        WebComponent c = new WebComponent("slider");
        c.setOutputMarkupId(true);
        slider_id = c.getMarkupId();
        add(c);

        // Write out the javascript to initialize the slider
        add(new AbstractBehavior() {
            @Override
            public void renderHead(IHeaderResponse response) {
                IRequestTarget target = RequestCycle.get().getRequestTarget();

                if (target instanceof AjaxRequestTarget) {
                    // If the target is an ajax request then we need
                    // to execute just the slider js.
                    AjaxRequestTarget t = (AjaxRequestTarget) target;
                    t.appendJavascript(init_slider_js());
                } else {
                    // Otherwise render the slider when the document is ready.

response.renderJavascript(init_slider_when_doc_ready_js(), null);
                }
            }
        });

    }

    private String init_slider_js() {
        return "$('#" + slider_id + "').slider({min: 0, max: 1, step: 0.1});";
    }

    private String init_slider_when_doc_ready_js() {
        return "$(document).ready(function() {" + init_slider_js() + "});";
    }
}

Regards,
Peter

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to