diff --git a/lib/assets/javascripts/react_ujs.js b/lib/assets/javascripts/react_ujs.js index dbffafa7b..47b184b79 100644 --- a/lib/assets/javascripts/react_ujs.js +++ b/lib/assets/javascripts/react_ujs.js @@ -36,17 +36,7 @@ } }; - // Register page load & unload events - if ($) { - $(mountReactComponents); - $(window).unload(unmountReactComponents); - } else { - document.addEventListener('DOMContentLoaded', mountReactComponents); - window.addEventListener('unload', unmountReactComponents); - } - - // Turbolinks specified events - if (typeof Turbolinks !== 'undefined') { + var handleTurbolinksEvents = function() { var handleEvent; if ($) { handleEvent = function(eventName, callback) { @@ -59,5 +49,17 @@ } handleEvent('page:change', mountReactComponents); handleEvent('page:before-change', unmountReactComponents); - } + }; + + var handleNativeEvents = function() { + if ($) { + $(mountReactComponents); + $(window).unload(unmountReactComponents); + } else { + document.addEventListener('DOMContentLoaded', mountReactComponents); + window.addEventListener('unload', unmountReactComponents); + } + }; + + typeof Turbolinks !== 'undefined' ? handleTurbolinksEvents() : handleNativeEvents(); })(document, window, React);