From fd6168994337d61d48a67cc57b11502945fc4301 Mon Sep 17 00:00:00 2001 From: Ryan Woods Date: Thu, 22 Dec 2022 08:47:08 +0100 Subject: [PATCH] squash Improve variantAutocomplete to be more reusable Make variantAutocomplete backwards compatible. Users can still pass an object of parameters, null or nothing, if the former is done, a console.warn deprecation warning is shown. --- .../spree/backend/variant_autocomplete.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/backend/app/assets/javascripts/spree/backend/variant_autocomplete.js b/backend/app/assets/javascripts/spree/backend/variant_autocomplete.js index 9c0bd758a0f..f0f0a93b45c 100644 --- a/backend/app/assets/javascripts/spree/backend/variant_autocomplete.js +++ b/backend/app/assets/javascripts/spree/backend/variant_autocomplete.js @@ -10,16 +10,20 @@ /** * Make the element a select2 dropdown used for finding Variants. By default, the search term will be * passed to the defined Spree::Config.variant_search_class by the controller with its defined scope. - * @param {Object} options Options + * @param {Object|undefined|null} options Options * @param {Function|undefined} options.searchParameters Returns a hash object for params to merge on the select2 ajax request * Accepts an argument of the select2 search term. To use Ransack, define * variant_search_term as a falsy value, and q as the Ransack query. Note, * you need to ensure that the attributes are allowed to be Ransacked. */ - $.fn.variantAutocomplete = function(options = {}) { + $.fn.variantAutocomplete = function(options) { function extraParameters(term) { - if (typeof(options['searchParameters']) === 'function') { - return options['searchParameters'](term) + if (typeof(options) === 'object') { + if (typeof(options['searchParameters']) === 'function') { + return options['searchParameters'](term) + } else { + console.warn("Solidus deprecation: Passing an object of parameters to variantAutocomplete is deprecated. Instead, on the options object, please declare `searchParameters` as a function returning the parameters.") + } } return {}