|
42 | 42 | } |
43 | 43 | }, |
44 | 44 |
|
| 45 | + // Get the constructor for a className |
| 46 | + getConstructor: function(className) { |
| 47 | + // Assume className is simple and can be found at top-level (window). |
| 48 | + // Fallback to eval to handle cases like 'My.React.ComponentName'. |
| 49 | + // Also, try to gracefully import Babel 6 style default exports |
| 50 | + // |
| 51 | + var constructor; |
| 52 | + |
| 53 | + // Try to access the class globally first |
| 54 | + constructor = window[className]; |
| 55 | + |
| 56 | + // If that didn't work, try eval |
| 57 | + if (!constructor) { |
| 58 | + constructor = eval.call(window, className); |
| 59 | + } |
| 60 | + |
| 61 | + // Lastly, if there is a default attribute try that |
| 62 | + if (constructor && constructor.default) { |
| 63 | + constructor = constructor.default; |
| 64 | + } |
| 65 | + |
| 66 | + return constructor; |
| 67 | + }, |
| 68 | + |
45 | 69 | // Within `searchSelector`, find nodes which should have React components |
46 | 70 | // inside them, and mount them with their props. |
47 | 71 | mountComponents: function(searchSelector) { |
|
50 | 74 | for (var i = 0; i < nodes.length; ++i) { |
51 | 75 | var node = nodes[i]; |
52 | 76 | var className = node.getAttribute(window.ReactRailsUJS.CLASS_NAME_ATTR); |
53 | | - |
54 | | - // Assume className is simple and can be found at top-level (window). |
55 | | - // Fallback to eval to handle cases like 'My.React.ComponentName'. |
56 | | - var constructor = window[className] || eval.call(window, className); |
| 77 | + var constructor = this.getConstructor(className); |
57 | 78 | var propsJson = node.getAttribute(window.ReactRailsUJS.PROPS_ATTR); |
58 | 79 | var props = propsJson && JSON.parse(propsJson); |
59 | 80 |
|
|
0 commit comments