Sha256: eb5578fa4a616f0f69ec8571ea3ab6a93ce5db936d1fe19c7e1e4d861fff2120

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 KB

Contents

(function() {
  var variantTemplate = HandlebarsTemplates["variants/autocomplete"];

  var formatVariantResult = function(variant) {
    return variantTemplate({
      variant: variant
    });
  };

  $.fn.variantAutocomplete = function(searchOptions) {
    if (searchOptions == null) {
      searchOptions = {};
    }
    this.select2({
      placeholder: Spree.translations.variant_placeholder,
      minimumInputLength: 3,
      initSelection: function(element, callback) {
        Spree.ajax({
          url: Spree.pathFor('api/variants/' + element.val()),
          success: callback
        });
      },
      ajax: {
        url: Spree.pathFor('api/variants'),
        datatype: "json",
        quietMillis: 500,
        params: {
          "headers": {
            'Authorization': 'Bearer ' + Spree.api_key
          }
        },
        data: function(term, page) {
          var searchData = {
            variant_search_term: term,
            token: Spree.api_key
          };
          return _.extend(searchData, searchOptions);
        },

        results: function(data, page) {
          window.variants = data["variants"];
          return {
            results: data["variants"]
          };
        }
      },

      formatResult: formatVariantResult,
      formatSelection: function(variant, container, escapeMarkup) {
        if (variant.options_text) {
          return Select2.util.escapeMarkup(variant.name + " (" + variant.options_text + ")");
        } else {
          return Select2.util.escapeMarkup(variant.name);
        }
      }
    });
  };
})();

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
solidus_backend-3.2.2 app/assets/javascripts/spree/backend/variant_autocomplete.js
solidus_backend-3.2.1 app/assets/javascripts/spree/backend/variant_autocomplete.js
solidus_backend-3.2.0 app/assets/javascripts/spree/backend/variant_autocomplete.js
solidus_backend-3.2.0.alpha app/assets/javascripts/spree/backend/variant_autocomplete.js