|
12 | 12 | RAILS_ENV_DEVELOPMENT: <%= Rails.env == "development" %>, |
13 | 13 | // helper method for the mount and unmount methods to find the |
14 | 14 | // `data-react-class` DOM elements |
15 | | - findDOMNodes: function() { |
| 15 | + findDOMNodes: function(searchSelector) { |
16 | 16 | // we will use fully qualified paths as we do not bind the callbacks |
17 | | - var selector = '[' + window.ReactRailsUJS.CLASS_NAME_ATTR + ']'; |
| 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 + ']'; |
| 22 | + } |
18 | 23 |
|
19 | 24 | if ($) { |
20 | 25 | return $(selector); |
|
23 | 28 | } |
24 | 29 | }, |
25 | 30 |
|
26 | | - mountComponents: function() { |
27 | | - var nodes = window.ReactRailsUJS.findDOMNodes(); |
| 31 | + mountComponents: function(searchSelector) { |
| 32 | + var nodes = window.ReactRailsUJS.findDOMNodes(searchSelector); |
28 | 33 |
|
29 | 34 | for (var i = 0; i < nodes.length; ++i) { |
30 | 35 | var node = nodes[i]; |
|
40 | 45 | } |
41 | 46 | }, |
42 | 47 |
|
43 | | - unmountComponents: function() { |
44 | | - var nodes = window.ReactRailsUJS.findDOMNodes(); |
| 48 | + unmountComponents: function(searchSelector) { |
| 49 | + var nodes = window.ReactRailsUJS.findDOMNodes(searchSelector); |
45 | 50 |
|
46 | 51 | for (var i = 0; i < nodes.length; ++i) { |
47 | 52 | var node = nodes[i]; |
|
77 | 82 | console.warn('The Turbolinks cache has been disabled (Turbolinks >= 2.4.0 is recommended). See https://github.com/reactjs/react-rails/issues/87 for more information.'); |
78 | 83 | } |
79 | 84 | } |
80 | | - handleEvent('page:change', window.ReactRailsUJS.mountComponents); |
81 | | - handleEvent(unmountEvent, window.ReactRailsUJS.unmountComponents); |
| 85 | + handleEvent('page:change', function() {window.ReactRailsUJS.mountComponents()}); |
| 86 | + handleEvent(unmountEvent, function() {window.ReactRailsUJS.unmountComponents()}); |
82 | 87 | } |
83 | 88 |
|
84 | 89 | function handleNativeEvents() { |
85 | 90 | if ($) { |
86 | | - $(window.ReactRailsUJS.mountComponents); |
87 | | - $(window).unload(window.ReactRailsUJS.unmountComponents); |
| 91 | + $(function() {window.ReactRailsUJS.mountComponents()}); |
| 92 | + $(window).unload(function() {window.ReactRailsUJS.unmountComponents()}); |
88 | 93 | } else { |
89 | | - document.addEventListener('DOMContentLoaded', window.ReactRailsUJS.mountComponents); |
90 | | - window.addEventListener('unload', window.ReactRailsUJS.unmountComponents); |
| 94 | + document.addEventListener('DOMContentLoaded', function() {window.ReactRailsUJS.mountComponents()}); |
| 95 | + window.addEventListener('unload', function() {window.ReactRailsUJS.unmountComponents()}); |
91 | 96 | } |
92 | 97 | } |
93 | 98 |
|
|
0 commit comments