Sha256: 9e6a27040eec783ca37cf2dcb3e127fa86a6c1de8c67823c88e991d77b6a2f7a

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

<%#encoding: UTF-8%>
// variant autocompletion

$(document).ready(function() {

  if ($('#variant_autocomplete_template').length > 0) {
    window.variantTemplate = Handlebars.compile($('#variant_autocomplete_template').text());
    window.variantStockTemplate = Handlebars.compile($('#variant_autocomplete_stock_template').text());
  }
});

formatVariantResult = function(variant) {
  if (variant["images"][0] != undefined && variant["images"][0].mini_url != undefined) {
    variant.image = variant.images[0].mini_url
  }
  return variantTemplate({ variant: variant })
}

$.fn.variantAutocomplete = function() {
  this.parent().children(".options_placeholder").prop('id', this.parent().data('index'))
  this.select2({
    placeholder: Spree.translations.variant_placeholder,
    minimumInputLength: 3,
    ajax: {
      url: Spree.url(Spree.routes.variants_search),
      datatype: 'json',
      data: function(term, page) {
        return {
          q: {
            "product_name_or_sku_cont": term
          }
        }
      },
      results: function (data, page) {
        window.variants = data['variants'];

        return { results: data['variants'] }
      }
    },
    formatResult: formatVariantResult,
    formatSelection: function (variant) {
      $(this.element).parent().children('.options_placeholder').html(variant.options_text)
      return variant.name;
    }
  })
}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spree_backend-2.2.3 app/assets/javascripts/spree/backend/variant_autocomplete.js.erb
spree_backend-2.2.2 app/assets/javascripts/spree/backend/variant_autocomplete.js.erb