From a49551b83ab303a31ca00b351e3ce7cf139cddb2 Mon Sep 17 00:00:00 2001 From: Andrew Mykhalchuk Date: Fri, 12 Jul 2024 12:57:43 +0300 Subject: [PATCH 1/2] Run select2('destroy') inside the loop to ensure it attempts to destroy the element only if it's present in the DOM --- app/assets/javascripts/spree/backend/spree-select2.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/spree/backend/spree-select2.js b/app/assets/javascripts/spree/backend/spree-select2.js index fb0e67283f..9af49d8f88 100644 --- a/app/assets/javascripts/spree/backend/spree-select2.js +++ b/app/assets/javascripts/spree/backend/spree-select2.js @@ -1,10 +1,12 @@ // we need to delete select2 instances before document is saved to cache // https://stackoverflow.com/questions/36497723/select2-with-ajax-gets-initialized-several-times-with-rails-turbolinks-events document.addEventListener("turbo:before-cache", function() { - $('select.select2').select2('destroy') - $('select.select2-clear').select2('destroy') + $('select.select2, select.select2-clear').each(function() { + $(this).select2('destroy') + }) }) + document.addEventListener("spree:load", function() { // Initiate a standard Select2 on any select element with the class .select2 // Remember to add a place holder in the HTML as needed. From a23a7fb47e17a086070c1654d2e787853e73efc0 Mon Sep 17 00:00:00 2001 From: Andrew Mykhalchuk Date: Fri, 12 Jul 2024 12:59:46 +0300 Subject: [PATCH 2/2] Remove new line --- app/assets/javascripts/spree/backend/spree-select2.js | 1 - 1 file changed, 1 deletion(-) diff --git a/app/assets/javascripts/spree/backend/spree-select2.js b/app/assets/javascripts/spree/backend/spree-select2.js index 9af49d8f88..2868138f73 100644 --- a/app/assets/javascripts/spree/backend/spree-select2.js +++ b/app/assets/javascripts/spree/backend/spree-select2.js @@ -6,7 +6,6 @@ document.addEventListener("turbo:before-cache", function() { }) }) - document.addEventListener("spree:load", function() { // Initiate a standard Select2 on any select element with the class .select2 // Remember to add a place holder in the HTML as needed.