javascript: expose Turbolinks globally

When calling `redirect_to` in a Rails controller that emits Javascript,
Rails will emit `Turbolinks.replace(…)` commands. And for this,
Turbolinks needs to be globally available.
This commit is contained in:
Pierre de La Morinerie 2019-12-03 15:13:20 +00:00
parent 97af50c700
commit f351ade434

View file

@ -65,19 +65,14 @@ Rails.start();
Turbolinks.start();
ActiveStorage.start();
// If Turbolinks is imported via Webpacker (and thus not available globally),
// ReactRailsUJS will be unable to locate it.
// https://github.com/reactjs/react-rails#event-handling
// eslint-disable-next-line no-undef
ReactRailsUJS.useContext(require.context('components', true));
// Add Turbolinks to the global namespace:
window.Turbolinks = Turbolinks;
// Remove previous event handlers and add new ones:
ReactRailsUJS.detectEvents();
// (Optional) Clean up global namespace:
delete window.Turbolinks;
// Expose globals
window.DS = window.DS || DS;
window.Chartkick = Chartkick;
// (Both Rails redirects and ReactRailsUJS expect Turbolinks to be globally available)
window.Turbolinks = Turbolinks;
// Now that Turbolinks is globally exposed,configure ReactRailsUJS
// eslint-disable-next-line no-undef
ReactRailsUJS.useContext(require.context('components', true));
// Remove previous event handlers and add new ones:
ReactRailsUJS.detectEvents();