Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

Commit 2a4ef6d

Browse files
committed
move api to ReactRailsUJS and update tests
1 parent d367169 commit 2a4ef6d

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

lib/assets/javascripts/react_ujs.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
// Unobtrusive scripting adapter for React
2-
(function(document, window, React) {
3-
2+
(function(document, window) {
43
// jQuery is optional. Use it to support legacy browsers.
54
var $ = (typeof window.jQuery !== 'undefined') && window.jQuery;
65

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 = {
108

119
CLASS_NAME_ATTR: 'data-react-class',
1210

@@ -17,32 +15,32 @@
1715
findDOMNodes: function() {
1816

1917
// 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 + ']';
2119

2220
if ($) return $(selector);
2321

2422
else return document.querySelectorAll(selector);
2523
},
2624

2725
mountComponents: function() {
28-
var nodes = React.ujs.findDOMNodes();
26+
var nodes = ReactRailsUJS.findDOMNodes();
2927

3028
for (var i = 0; i < nodes.length; ++i) {
3129
var node = nodes[i];
32-
var className = node.getAttribute(React.ujs.CLASS_NAME_ATTR);
30+
var className = node.getAttribute(window.ReactRailsUJS.CLASS_NAME_ATTR);
3331

3432
// Assume className is simple and can be found at top-level (window).
3533
// Fallback to eval to handle cases like 'My.React.ComponentName'.
3634
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);
3836
var props = propsJson && JSON.parse(propsJson);
3937

4038
React.render(React.createElement(constructor, props), node);
4139
}
4240
},
4341

4442
unmountComponents: function() {
45-
var nodes = React.ujs.findDOMNodes();
43+
var nodes = window.ReactRailsUJS.findDOMNodes();
4644

4745
for (var i = 0; i < nodes.length; ++i) {
4846
var node = nodes[i];
@@ -71,20 +69,20 @@
7169
};
7270

7371
}
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);
7674
}
7775

7876
function handleNativeEvents() {
7977
if ($) {
8078

81-
$(React.ujs.mountComponents);
82-
$(window).unload(React.ujs.unmountComponents);
79+
$(window.ReactRailsUJS.mountComponents);
80+
$(window).unload(window.ReactRailsUJS.unmountComponents);
8381

8482
} else {
8583

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);
8886
}
8987
}
9088

@@ -93,5 +91,4 @@
9391
} else {
9492
handleNativeEvents();
9593
}
96-
97-
})(document, window, React);
94+
})(document, window);

test/view_helper_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,21 @@ class ViewHelperTest < ActionDispatch::IntegrationTest
5454
assert page.has_content?('Hello Bob')
5555

5656
# the exposed ujs object is present
57-
ujs_present = page.evaluate_script('typeof React.ujs === "object";')
57+
ujs_present = page.evaluate_script('typeof ReactRailsUJS === "object";')
5858
assert_equal(ujs_present, true)
5959

6060
# it contains the constants
61-
class_name_present = page.evaluate_script('React.ujs.CLASS_NAME_ATTR === "data-react-class";')
61+
class_name_present = page.evaluate_script('ReactRailsUJS.CLASS_NAME_ATTR === "data-react-class";')
6262
assert_equal(class_name_present, true)
63-
props_present = page.evaluate_script('React.ujs.PROPS_ATTR === "data-react-props";')
63+
props_present = page.evaluate_script('ReactRailsUJS.PROPS_ATTR === "data-react-props";')
6464
assert_equal(props_present, true)
6565

6666
#it contains the methods
67-
find_dom_nodes_present = page.evaluate_script('typeof React.ujs.findDOMNodes === "function";')
67+
find_dom_nodes_present = page.evaluate_script('typeof ReactRailsUJS.findDOMNodes === "function";')
6868
assert_equal(find_dom_nodes_present, true)
69-
mount_components_present = page.evaluate_script('typeof React.ujs.mountComponents === "function";')
69+
mount_components_present = page.evaluate_script('typeof ReactRailsUJS.mountComponents === "function";')
7070
assert_equal(mount_components_present, true)
71-
unmount_components_present = page.evaluate_script('typeof React.ujs.unmountComponents === "function";')
71+
unmount_components_present = page.evaluate_script('typeof ReactRailsUJS.unmountComponents === "function";')
7272
assert_equal(unmount_components_present, true)
7373
end
7474

0 commit comments

Comments
 (0)