Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix The select2('destroy') method was called on an element that is not using Select2 #171

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/assets/javascripts/spree/backend/global/flatpickr.es6
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ document.addEventListener("spree:load", function() {

document.addEventListener("turbo:before-cache", function() {
document.querySelectorAll('.datePickerFrom, .datePickerTo, .datepicker').forEach(function(element) {
element._flatpickr.destroy()
if (element._flatpickr) {
element._flatpickr.destroy()
}
})
})

Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,19 @@ document.addEventListener("spree:load", function() {

// 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() {
const select2Autocompletes = document.querySelectorAll('select[data-autocomplete-url-value]')
select2Autocompletes.forEach(element => $(element).select2('destroy'))
document.addEventListener('turbo:before-cache', function () {
const select2Autocompletes = document.querySelectorAll(
'select[data-autocomplete-url-value]'
)
select2Autocompletes.forEach(function (element) {
const jqElement = $(element)
if (
jqElement.data('select2') ||
jqElement.hasClass('select2-hidden-accessible')
) {
jqElement.select2('destroy')
}
})
})

// eslint-disable-next-line no-unused-vars
Expand Down
26 changes: 23 additions & 3 deletions app/assets/javascripts/spree/backend/spree-select2.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
// 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')
document.addEventListener('turbo:before-cache', function () {
const isSelect2JqElement = function (jqElement) {
return (
jqElement.data('select2') ||
jqElement.hasClass('select2-hidden-accessible')
)
}

const select2Elements = document.querySelectorAll('select.select2')
select2Elements.forEach(function (element) {
const jqElement = $(element)
if (isSelect2JqElement(jqElement)) {
jqElement.select2('destroy')
}
})

const select2ClearElements = document.querySelectorAll('select.select2-clear')
select2ClearElements.forEach(function (element) {
const jqElement = $(element)
if (isSelect2JqElement(jqElement)) {
jqElement.select2('destroy')
}
})
})

document.addEventListener("spree:load", function() {
Expand Down