Skip to content

Commit dc08d65

Browse files
committed
replace ujs file itself
1 parent d2360f9 commit dc08d65

File tree

2 files changed

+72
-138
lines changed

2 files changed

+72
-138
lines changed
Lines changed: 72 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,97 @@
11
// Unobtrusive scripting adapter for React
22
(function(document, window, React) {
3-
var CLASS_NAME_ATTR = 'data-react-class';
4-
var PROPS_ATTR = 'data-react-props';
5-
3+
64
// jQuery is optional. Use it to support legacy browsers.
7-
var $ = (typeof jQuery !== 'undefined') && jQuery;
8-
9-
var findReactDOMNodes = function() {
10-
var SELECTOR = '[' + CLASS_NAME_ATTR + ']';
11-
if ($) {
12-
return $(SELECTOR);
13-
} else {
14-
return document.querySelectorAll(SELECTOR);
15-
}
16-
};
17-
18-
var mountReactComponents = function() {
19-
var nodes = findReactDOMNodes();
20-
for (var i = 0; i < nodes.length; ++i) {
21-
var node = nodes[i];
22-
var className = node.getAttribute(CLASS_NAME_ATTR);
23-
// Assume className is simple and can be found at top-level (window).
24-
// Fallback to eval to handle cases like 'My.React.ComponentName'.
25-
var constructor = window[className] || eval.call(window, className);
26-
var propsJson = node.getAttribute(PROPS_ATTR);
27-
var props = propsJson && JSON.parse(propsJson);
28-
React.render(React.createElement(constructor, props), node);
29-
}
30-
};
31-
32-
var unmountReactComponents = function() {
33-
var nodes = findReactDOMNodes();
34-
for (var i = 0; i < nodes.length; ++i) {
35-
React.unmountComponentAtNode(nodes[i]);
5+
var $ = (typeof window.jQuery !== 'undefined') && window.jQuery;
6+
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 = {
10+
11+
CLASS_NAME_ATTR: 'data-react-class',
12+
13+
PROPS_ATTR: 'data-react-props',
14+
15+
// helper method for the mount and unmount methods to find the
16+
// `data-react-class` DOM elements
17+
findDOMNodes: function() {
18+
19+
// we will use fully qualified paths as we do not bind the callbacks
20+
var selector = '[' + React.ujs.CLASS_NAME_ATTR + ']';
21+
22+
if ($) return $(selector);
23+
24+
else return document.querySelectorAll(selector);
25+
},
26+
27+
mountComponents: function() {
28+
var nodes = React.ujs.findDOMNodes();
29+
30+
for (var i = 0; i < nodes.length; ++i) {
31+
var node = nodes[i];
32+
var className = node.getAttribute(React.ujs.CLASS_NAME_ATTR);
33+
34+
// Assume className is simple and can be found at top-level (window).
35+
// Fallback to eval to handle cases like 'My.React.ComponentName'.
36+
var constructor = window[className] || eval.call(window, className);
37+
var propsJson = node.getAttribute(React.ujs.PROPS_ATTR);
38+
var props = propsJson && JSON.parse(propsJson);
39+
40+
React.render(React.createElement(constructor, props), node);
41+
}
42+
},
43+
44+
unmountComponents: function() {
45+
var nodes = React.ujs.findDOMNodes();
46+
47+
for (var i = 0; i < nodes.length; ++i) {
48+
var node = nodes[i];
49+
50+
React.unmountComponentAtNode(node);
51+
// now remove the `data-react-class` wrapper as well
52+
node.parentElement && node.parentElement.removeChild(node);
53+
}
3654
}
3755
};
3856

39-
var handleTurbolinksEvents = function() {
57+
// functions not exposed publicly
58+
function handleTurbolinksEvents () {
4059
var handleEvent;
60+
4161
if ($) {
62+
4263
handleEvent = function(eventName, callback) {
4364
$(document).on(eventName, callback);
44-
}
65+
};
66+
4567
} else {
68+
4669
handleEvent = function(eventName, callback) {
4770
document.addEventListener(eventName, callback);
48-
}
71+
};
72+
4973
}
50-
handleEvent('page:change', mountReactComponents);
51-
handleEvent('page:receive', unmountReactComponents);
52-
};
74+
handleEvent('page:change', React.ujs.mountComponents);
75+
handleEvent('page:receive', React.ujs.unmountComponents);
76+
}
5377

54-
var handleNativeEvents = function() {
78+
function handleNativeEvents() {
5579
if ($) {
56-
$(mountReactComponents);
57-
$(window).unload(unmountReactComponents);
80+
81+
$(React.ujs.mountComponents);
82+
$(window).unload(React.ujs.unmountComponents);
83+
5884
} else {
59-
document.addEventListener('DOMContentLoaded', mountReactComponents);
60-
window.addEventListener('unload', unmountReactComponents);
85+
86+
document.addEventListener('DOMContentLoaded', React.ujs.mountComponents);
87+
window.addEventListener('unload', React.ujs.unmountComponents);
6188
}
62-
};
89+
}
6390

6491
if (typeof Turbolinks !== 'undefined' && Turbolinks.supported) {
6592
handleTurbolinksEvents();
6693
} else {
6794
handleNativeEvents();
6895
}
96+
6997
})(document, window, React);

lib/assets/javascripts/react_ujs_p.js

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)