@Override
  public void renderHead(IHeaderResponse response) {
    super.renderHead(response);

    // initialize select2

    response.render(
        OnDomReadyHeaderItem.forScript(
            JQuery.execute("$('#%s').select2(%s);", getJquerySafeMarkupId(), settings.toJson())));

    // select current value

    renderInitializationScript(response);
  }
  @Override
  protected void onInitialize() {
    super.onInitialize();

    // configure the ajax callbacks

    AjaxSettings ajax = settings.getAjax(true);

    ajax.setData(
        String.format(
            "function(term, page) { return { term: term, page:page, '%s':true, '%s':[window.location.protocol, '//', window.location.host, window.location.pathname].join('')}; }",
            WebRequest.PARAM_AJAX, WebRequest.PARAM_AJAX_BASE_URL));

    ajax.setResults("function(data, page) { return data; }");
  }
  @Override
  public void onEvent(IEvent<?> event) {
    super.onEvent(event);

    if (event.getPayload() instanceof AjaxRequestTarget) {

      AjaxRequestTarget target = (AjaxRequestTarget) event.getPayload();

      if (target.getComponents().contains(this)) {

        // if this component is being repainted by ajax, directly, we must destroy Select2 so it
        // removes
        // its elements from DOM

        target.prependJavaScript(
            JQuery.execute("$('#%s').select2('destroy');", getJquerySafeMarkupId()));
      }
    }
  }
 @Override
 protected void onDetach() {
   provider.detach();
   super.onDetach();
 }
  @Override
  protected void onConfigure() {
    super.onConfigure();

    getSettings().getAjax().setUrl(urlFor(IResourceListener.INTERFACE, null));
  }