Sha256: 8998dde54860db0d55c634d87694f2ef15e66e483d6ab2d09a975fddfe912b67

Contents?: true

Size: 1.46 KB

Versions: 8

Compression:

Stored size: 1.46 KB

Contents

//= require select2
$(document).ready(function() {
  $.fn.select2.defaults.set("theme", "bootstrap");
  $.fn.select2.defaults.set("ajax", {
    cache: true,
    dataType: "json",
    delay: 300,
    data: transformRequest,
    processResults: transformResponse
  });
  $.fn.select2.defaults.set("minimumInputLength", 2);
  $.fn.select2.defaults.set("allowClear", true);

  function transformRequest(params) {
    var page = params.page || 0;
    var page_size = this.data('kadmin--page-size') || 10;
    var filter_param = this.data('kadmin--filter-param') || null;
    var options = {
      page_offset: page * page_size,
      page_size: page_size
    };

    if (filter_param) {
      options[filter_param] = params.term;
    }

    return options;
  }

  function transformResponse(data, params) {
    var items = data.data || data.items;
    var options = this.options.options.kadmin || {}; // weird but it is what it is
    var displayProperty = options.displayProperty || 'text';
    var valueProperty = options.valueProperty || 'id';

    var results = [];
    $(items).each(function(index, item) {
      results.push({ text: item[displayProperty], id: item[valueProperty] });
    });

    return {
      results: results,
      pagination: { more: data.more }
    };
  }

  // Need to delay a bit otherwise we have issues
  // TODO: Figure out why this happens?
  setTimeout(function() {
    $('select.kadmin-select2').each(function() {
      $(this).select2();
    });
  }, 300);
});

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
kadmin-0.6.7 app/assets/javascripts/kadmin/select2.js
kadmin-0.6.6 app/assets/javascripts/kadmin/select2.js
kadmin-0.6.5 app/assets/javascripts/kadmin/select2.js
kadmin-0.6.4 app/assets/javascripts/kadmin/select2.js
kadmin-0.6.3 app/assets/javascripts/kadmin/select2.js
kadmin-0.6.1 app/assets/javascripts/kadmin/select2.js
kadmin-0.5.7 app/assets/javascripts/kadmin/select2.js
kadmin-0.5.6 app/assets/javascripts/kadmin/select2.js