Sha256: f4effbc0499f2ac06869fd635edcb1ac0caea6939ae0362e6ecc1bd9b7205d5b

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 KB

Contents

$.fn.productAutocomplete = function (options) {
  'use strict';

  // Default options
  options = options || {};
  var multiple = typeof(options.multiple) !== 'undefined' ? options.multiple : true;

  function formatProduct(product) {
    return Select2.util.escapeMarkup(product.name);
  }

  this.select2({
    minimumInputLength: 3,
    multiple: multiple,
    initSelection: function (element, callback) {
      $.get(Spree.routes.product_search, {
        ids: element.val().split(','),
        token: Spree.api_key
      }, function (data) {
        callback(multiple ? data.products : data.products[0]);
      });
    },
    ajax: {
      url: Spree.routes.product_search,
      datatype: 'json',
      data: function (term, page) {
        return {
          q: {
            name_cont: term,
            sku_cont: term
          },
          m: 'OR',
          token: Spree.api_key
        };
      },
      results: function (data, page) {
        var products = data.products ? data.products : [];
        return {
          results: products
        };
      }
    },
    formatResult: formatProduct,
    formatSelection: formatProduct
  });
};

$(document).ready(function () {
  $('.product_picker').productAutocomplete();
});

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spree_backend-3.0.10 app/assets/javascripts/spree/backend/product_picker.js
spree_backend-3.0.9 app/assets/javascripts/spree/backend/product_picker.js
spree_backend-3.0.8 app/assets/javascripts/spree/backend/product_picker.js
spree_backend-3.0.7 app/assets/javascripts/spree/backend/product_picker.js