Skip to content

Commit

Permalink
Ensure select2 is initialized regardless of page load events
Browse files Browse the repository at this point in the history
Previously, select2 was only loaded successfully, if first the page was loaded (turbolinks:load) and then all select2 locales (select2:locales:loaded). However, if the order was different, select2 wouldn't be initialized.

Fixes #1476
  • Loading branch information
MrSerth committed Sep 25, 2024
1 parent 504416d commit 981dae4
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions app/javascript/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@ import locales from "../../tmp/locales.json";

Promise.all(
Object.keys(locales).map(locale => import(`select2/dist/js/i18n/${locale}`))
).then(() =>
document.dispatchEvent(new Event("select2:locales:loaded"))
).then(() => {
// Since there is a race condition between the locales and turbolinks,
// we don't know whether the locales are loaded before or after turbolinks.
// Therefore, we trigger the event in both cases.
$(document).on('turbolinks:load', () =>
$(document).trigger('select2:locales:loaded')
);
$(document).trigger('select2:locales:loaded')
}
);

// Fetch user locale from html#lang.
Expand Down

0 comments on commit 981dae4

Please sign in to comment.