|
1 | 1 | // Unobtrusive scripting adapter for React
|
2 |
| -(function(document, window, React) { |
3 |
| - |
| 2 | +(function(document, window) { |
4 | 3 | // jQuery is optional. Use it to support legacy browsers.
|
5 | 4 | var $ = (typeof window.jQuery !== 'undefined') && window.jQuery;
|
6 | 5 |
|
7 |
| - // rather than create another namespace just for the 3 methods and 2 |
8 |
| - // properties we expose just append them to the `React.ujs` object |
9 |
| - React.ujs = { |
| 6 | + // create the namespace |
| 7 | + window.ReactRailsUJS = { |
10 | 8 |
|
11 | 9 | CLASS_NAME_ATTR: 'data-react-class',
|
12 | 10 |
|
|
17 | 15 | findDOMNodes: function() {
|
18 | 16 |
|
19 | 17 | // we will use fully qualified paths as we do not bind the callbacks
|
20 |
| - var selector = '[' + React.ujs.CLASS_NAME_ATTR + ']'; |
| 18 | + var selector = '[' + window.ReactRailsUJS.CLASS_NAME_ATTR + ']'; |
21 | 19 |
|
22 | 20 | if ($) return $(selector);
|
23 | 21 |
|
24 | 22 | else return document.querySelectorAll(selector);
|
25 | 23 | },
|
26 | 24 |
|
27 | 25 | mountComponents: function() {
|
28 |
| - var nodes = React.ujs.findDOMNodes(); |
| 26 | + var nodes = ReactRailsUJS.findDOMNodes(); |
29 | 27 |
|
30 | 28 | for (var i = 0; i < nodes.length; ++i) {
|
31 | 29 | var node = nodes[i];
|
32 |
| - var className = node.getAttribute(React.ujs.CLASS_NAME_ATTR); |
| 30 | + var className = node.getAttribute(window.ReactRailsUJS.CLASS_NAME_ATTR); |
33 | 31 |
|
34 | 32 | // Assume className is simple and can be found at top-level (window).
|
35 | 33 | // Fallback to eval to handle cases like 'My.React.ComponentName'.
|
36 | 34 | var constructor = window[className] || eval.call(window, className);
|
37 |
| - var propsJson = node.getAttribute(React.ujs.PROPS_ATTR); |
| 35 | + var propsJson = node.getAttribute(window.ReactRailsUJS.PROPS_ATTR); |
38 | 36 | var props = propsJson && JSON.parse(propsJson);
|
39 | 37 |
|
40 | 38 | React.render(React.createElement(constructor, props), node);
|
41 | 39 | }
|
42 | 40 | },
|
43 | 41 |
|
44 | 42 | unmountComponents: function() {
|
45 |
| - var nodes = React.ujs.findDOMNodes(); |
| 43 | + var nodes = window.ReactRailsUJS.findDOMNodes(); |
46 | 44 |
|
47 | 45 | for (var i = 0; i < nodes.length; ++i) {
|
48 | 46 | var node = nodes[i];
|
|
71 | 69 | };
|
72 | 70 |
|
73 | 71 | }
|
74 |
| - handleEvent('page:change', React.ujs.mountComponents); |
75 |
| - handleEvent('page:receive', React.ujs.unmountComponents); |
| 72 | + handleEvent('page:change', window.ReactRailsUJS.mountComponents); |
| 73 | + handleEvent('page:receive', window.ReactRailsUJS.unmountComponents); |
76 | 74 | }
|
77 | 75 |
|
78 | 76 | function handleNativeEvents() {
|
79 | 77 | if ($) {
|
80 | 78 |
|
81 |
| - $(React.ujs.mountComponents); |
82 |
| - $(window).unload(React.ujs.unmountComponents); |
| 79 | + $(window.ReactRailsUJS.mountComponents); |
| 80 | + $(window).unload(window.ReactRailsUJS.unmountComponents); |
83 | 81 |
|
84 | 82 | } else {
|
85 | 83 |
|
86 |
| - document.addEventListener('DOMContentLoaded', React.ujs.mountComponents); |
87 |
| - window.addEventListener('unload', React.ujs.unmountComponents); |
| 84 | + document.addEventListener('DOMContentLoaded', window.ReactRailsUJS.mountComponents); |
| 85 | + window.addEventListener('unload', window.ReactRailsUJS.unmountComponents); |
88 | 86 | }
|
89 | 87 | }
|
90 | 88 |
|
|
93 | 91 | } else {
|
94 | 92 | handleNativeEvents();
|
95 | 93 | }
|
96 |
| - |
97 |
| -})(document, window, React); |
| 94 | +})(document, window); |
0 commit comments