Skip to content

Commit e969d28

Browse files
committed
Ensure select2 is initialized regardless of page load events
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
1 parent af90f74 commit e969d28

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

app/javascript/application.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,15 @@ import locales from "../../tmp/locales.json";
5050

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

5764
// Fetch user locale from html#lang.

0 commit comments

Comments
 (0)