|
4 | 4 | (function(document, window) {
|
5 | 5 | // jQuery is optional. Use it to support legacy browsers.
|
6 | 6 | var $ = (typeof window.jQuery !== 'undefined') && window.jQuery;
|
7 |
| - |
| 7 | + |
8 | 8 | // create the namespace
|
9 | 9 | window.ReactRailsUJS = {
|
10 | 10 | CLASS_NAME_ATTR: 'data-react-class',
|
11 | 11 | PROPS_ATTR: 'data-react-props',
|
12 |
| - // helper method for the mount and unmount methods to find the |
| 12 | + RAILS_ENV_DEVELOPMENT: <%= Rails.env == "development" %>, |
| 13 | + // helper method for the mount and unmount methods to find the |
13 | 14 | // `data-react-class` DOM elements
|
14 | 15 | findDOMNodes: function() {
|
15 |
| - // we will use fully qualified paths as we do not bind the callbacks |
| 16 | + // we will use fully qualified paths as we do not bind the callbacks |
16 | 17 | var selector = '[' + window.ReactRailsUJS.CLASS_NAME_ATTR + ']';
|
17 |
| - |
| 18 | + |
18 | 19 | if ($) {
|
19 | 20 | return $(selector);
|
20 | 21 | } else {
|
21 | 22 | return document.querySelectorAll(selector);
|
22 | 23 | }
|
23 | 24 | },
|
24 |
| - |
| 25 | + |
25 | 26 | mountComponents: function() {
|
26 | 27 | var nodes = window.ReactRailsUJS.findDOMNodes();
|
27 |
| - |
| 28 | + |
28 | 29 | for (var i = 0; i < nodes.length; ++i) {
|
29 | 30 | var node = nodes[i];
|
30 | 31 | var className = node.getAttribute(window.ReactRailsUJS.CLASS_NAME_ATTR);
|
31 |
| - |
| 32 | + |
32 | 33 | // Assume className is simple and can be found at top-level (window).
|
33 | 34 | // Fallback to eval to handle cases like 'My.React.ComponentName'.
|
34 | 35 | var constructor = window[className] || eval.call(window, className);
|
35 | 36 | var propsJson = node.getAttribute(window.ReactRailsUJS.PROPS_ATTR);
|
36 | 37 | var props = propsJson && JSON.parse(propsJson);
|
37 |
| - |
| 38 | + |
38 | 39 | React.render(React.createElement(constructor, props), node);
|
39 | 40 | }
|
40 | 41 | },
|
41 |
| - |
| 42 | + |
42 | 43 | unmountComponents: function() {
|
43 | 44 | var nodes = window.ReactRailsUJS.findDOMNodes();
|
44 |
| - |
| 45 | + |
45 | 46 | for (var i = 0; i < nodes.length; ++i) {
|
46 | 47 | var node = nodes[i];
|
47 |
| - |
| 48 | + |
48 | 49 | React.unmountComponentAtNode(node);
|
49 | 50 | // now remove the `data-react-class` wrapper as well
|
50 | 51 | node.parentElement && node.parentElement.removeChild(node);
|
|
55 | 56 | // functions not exposed publicly
|
56 | 57 | function handleTurbolinksEvents () {
|
57 | 58 | var handleEvent;
|
58 |
| - |
| 59 | + var unmountEvent; |
| 60 | + |
59 | 61 | if ($) {
|
60 | 62 | handleEvent = function(eventName, callback) {
|
61 | 63 | $(document).on(eventName, callback);
|
62 | 64 | };
|
63 |
| - |
| 65 | + |
64 | 66 | } else {
|
65 | 67 | handleEvent = function(eventName, callback) {
|
66 | 68 | document.addEventListener(eventName, callback);
|
67 | 69 | };
|
68 | 70 | }
|
| 71 | + |
| 72 | + if (Turbolinks.EVENTS) { |
| 73 | + unmountEvent = Turbolinks.EVENTS.BEFORE_UNLOAD; |
| 74 | + } else { |
| 75 | + unmountEvent = 'page:receive'; |
| 76 | + Turbolinks.pagesCached(0); |
| 77 | + |
| 78 | + if (window.ReactRailsUJS.RAILS_ENV_DEVELOPMENT) { |
| 79 | + console.warn('The Turbolinks cache has been disabled (Turbolinks >= 2.4.0 is recommended). See https://github.com/reactjs/react-rails/issues/87 for more information.'); |
| 80 | + } |
| 81 | + } |
69 | 82 | handleEvent('page:change', window.ReactRailsUJS.mountComponents);
|
70 |
| - handleEvent('page:receive', window.ReactRailsUJS.unmountComponents); |
| 83 | + handleEvent(unmountEvent, window.ReactRailsUJS.unmountComponents); |
71 | 84 | }
|
72 | 85 |
|
73 | 86 | function handleNativeEvents() {
|
74 |
| - if ($) { |
| 87 | + if ($) { |
75 | 88 | $(window.ReactRailsUJS.mountComponents);
|
76 | 89 | $(window).unload(window.ReactRailsUJS.unmountComponents);
|
77 |
| - |
78 | 90 | } else {
|
79 | 91 | document.addEventListener('DOMContentLoaded', window.ReactRailsUJS.mountComponents);
|
80 | 92 | window.addEventListener('unload', window.ReactRailsUJS.unmountComponents);
|
|
0 commit comments