Skip to content

Commit 3560b5b

Browse files
aratakximus
authored andcommitted
Allow to pass DOM object as parent selector.
Extend possibility of `findDOMNodes` and as a consequence `mountComponents`. It allows to use it with an element which was inserted to the DOM tree without react.js. Example: ``` $(document).on('nested:fieldAdded', function(e) { window.ReactRailsUJS.mountComponents(e.target); }); ``` With backward compatibility, of course.
1 parent d7ecd8b commit 3560b5b

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

lib/assets/javascripts/react_ujs.js.erb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,29 @@
1414
// `data-react-class` DOM elements
1515
findDOMNodes: function(searchSelector) {
1616
// we will use fully qualified paths as we do not bind the callbacks
17-
var selector;
18-
if (typeof searchSelector === 'undefined') {
19-
var selector = '[' + window.ReactRailsUJS.CLASS_NAME_ATTR + ']';
20-
} else {
21-
var selector = searchSelector + ' [' + window.ReactRailsUJS.CLASS_NAME_ATTR + ']';
17+
var selector, parent;
18+
19+
switch (typeof searchSelector) {
20+
case 'undefined':
21+
selector = '[' + window.ReactRailsUJS.CLASS_NAME_ATTR + ']';
22+
parent = document;
23+
break;
24+
case 'object':
25+
selector = '[' + window.ReactRailsUJS.CLASS_NAME_ATTR + ']';
26+
parent = searchSelector;
27+
break;
28+
case 'string':
29+
selector = searchSelector + '[' + window.ReactRailsUJS.CLASS_NAME_ATTR + ']';
30+
parent = document;
31+
break
32+
default:
33+
break;
2234
}
2335

2436
if ($) {
25-
return $(selector);
37+
return $(selector, parent);
2638
} else {
27-
return document.querySelectorAll(selector);
39+
return parent.querySelectorAll(selector);
2840
}
2941
},
3042

0 commit comments

Comments
 (0)