|
| 1 | +/*globals React, Turbolinks*/ |
| 2 | + |
1 | 3 | // Unobtrusive scripting adapter for React |
2 | 4 | (function(document, window) { |
3 | 5 | // jQuery is optional. Use it to support legacy browsers. |
4 | 6 | var $ = (typeof window.jQuery !== 'undefined') && window.jQuery; |
5 | 7 |
|
6 | 8 | // create the namespace |
7 | 9 | window.ReactRailsUJS = { |
8 | | - |
9 | 10 | CLASS_NAME_ATTR: 'data-react-class', |
10 | | - |
11 | 11 | PROPS_ATTR: 'data-react-props', |
12 | | - |
13 | 12 | // helper method for the mount and unmount methods to find the |
14 | 13 | // `data-react-class` DOM elements |
15 | 14 | findDOMNodes: function() { |
16 | | - |
17 | 15 | // we will use fully qualified paths as we do not bind the callbacks |
18 | 16 | var selector = '[' + window.ReactRailsUJS.CLASS_NAME_ATTR + ']'; |
19 | 17 |
|
20 | | - if ($) return $(selector); |
21 | | - |
22 | | - else return document.querySelectorAll(selector); |
| 18 | + if ($) { |
| 19 | + return $(selector); |
| 20 | + } else { |
| 21 | + return document.querySelectorAll(selector); |
| 22 | + } |
23 | 23 | }, |
24 | 24 |
|
25 | 25 | mountComponents: function() { |
26 | | - var nodes = ReactRailsUJS.findDOMNodes(); |
| 26 | + var nodes = window.ReactRailsUJS.findDOMNodes(); |
27 | 27 |
|
28 | 28 | for (var i = 0; i < nodes.length; ++i) { |
29 | 29 | var node = nodes[i]; |
|
57 | 57 | var handleEvent; |
58 | 58 |
|
59 | 59 | if ($) { |
60 | | - |
61 | 60 | handleEvent = function(eventName, callback) { |
62 | 61 | $(document).on(eventName, callback); |
63 | 62 | }; |
64 | 63 |
|
65 | 64 | } else { |
66 | | - |
67 | 65 | handleEvent = function(eventName, callback) { |
68 | 66 | document.addEventListener(eventName, callback); |
69 | 67 | }; |
70 | | - |
71 | 68 | } |
72 | 69 | handleEvent('page:change', window.ReactRailsUJS.mountComponents); |
73 | 70 | handleEvent('page:receive', window.ReactRailsUJS.unmountComponents); |
74 | 71 | } |
75 | 72 |
|
76 | 73 | function handleNativeEvents() { |
77 | | - if ($) { |
78 | | - |
| 74 | + if ($) { |
79 | 75 | $(window.ReactRailsUJS.mountComponents); |
80 | 76 | $(window).unload(window.ReactRailsUJS.unmountComponents); |
81 | 77 |
|
82 | 78 | } else { |
83 | | - |
84 | 79 | document.addEventListener('DOMContentLoaded', window.ReactRailsUJS.mountComponents); |
85 | 80 | window.addEventListener('unload', window.ReactRailsUJS.unmountComponents); |
86 | 81 | } |
|
0 commit comments