Skip to content

Commit 53d9ad7

Browse files
author
Robert Mosolgo
authored
Merge pull request #503 from DNACodeStudios/babel6-default-modules
Update to accept new babel 6 module exports
2 parents d2dba07 + 426384b commit 53d9ad7

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

lib/assets/javascripts/react_ujs_mount.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@
4242
}
4343
},
4444

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+
4569
// Within `searchSelector`, find nodes which should have React components
4670
// inside them, and mount them with their props.
4771
mountComponents: function(searchSelector) {
@@ -50,10 +74,7 @@
5074
for (var i = 0; i < nodes.length; ++i) {
5175
var node = nodes[i];
5276
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);
5778
var propsJson = node.getAttribute(window.ReactRailsUJS.PROPS_ATTR);
5879
var props = propsJson && JSON.parse(propsJson);
5980

0 commit comments

Comments
 (0)