Comparing version 0.2.0 to 0.3.0
@@ -81,2 +81,30 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
/* 0 */ | ||
/***/ (function(module, exports) { | ||
// Assume className is simple and can be found at top-level (window). | ||
// Fallback to eval to handle cases like 'My.React.ComponentName'. | ||
// Also, try to gracefully import Babel 6 style default exports | ||
var topLevel = typeof window === "undefined" ? this : window; | ||
module.exports = function(className) { | ||
var constructor; | ||
// Try to access the class globally first | ||
constructor = topLevel[className]; | ||
// If that didn't work, try eval | ||
if (!constructor) { | ||
constructor = eval(className); | ||
} | ||
// Lastly, if there is a default attribute try that | ||
if (constructor && constructor['default']) { | ||
constructor = constructor['default']; | ||
} | ||
return constructor; | ||
} | ||
/***/ }), | ||
/* 1 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
@@ -123,53 +151,21 @@ | ||
/***/ }), | ||
/* 1 */ | ||
/***/ (function(module, exports) { | ||
/* 2 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
// Assume className is simple and can be found at top-level (window). | ||
// Fallback to eval to handle cases like 'My.React.ComponentName'. | ||
// Also, try to gracefully import Babel 6 style default exports | ||
var topLevel = typeof window === "undefined" ? this : window; | ||
// Make a function which: | ||
// - First tries to require the name | ||
// - Then falls back to global lookup | ||
var fromGlobal = __webpack_require__(0) | ||
var fromRequireContext = __webpack_require__(12) | ||
module.exports = function(className) { | ||
var constructor; | ||
// Try to access the class globally first | ||
constructor = topLevel[className]; | ||
// If that didn't work, try eval | ||
if (!constructor) { | ||
constructor = eval(className); | ||
} | ||
// Lastly, if there is a default attribute try that | ||
if (constructor && constructor['default']) { | ||
constructor = constructor['default']; | ||
} | ||
return constructor; | ||
} | ||
/***/ }), | ||
/* 2 */ | ||
/***/ (function(module, exports) { | ||
// Load React components by requiring them from "components/", for example: | ||
// | ||
// - "pages/index" -> `require("components/pages/index")` | ||
// - "pages/show.Header" -> `require("components/pages/show").Header` | ||
// - "pages/show.Body.Content" -> `require("components/pages/show").Body.Content` | ||
// | ||
module.exports = function(reqctx) { | ||
var fromCtx = fromRequireContext(reqctx) | ||
return function(className) { | ||
var parts = className.split(".") | ||
var filename = parts.shift() | ||
var keys = parts | ||
// Load the module: | ||
var component = reqctx("./" + filename) | ||
// Then access each key: | ||
keys.forEach(function(k) { | ||
component = component[k] | ||
}) | ||
// support `export default` | ||
if (component.__esModule) { | ||
component = component["default"] | ||
var component; | ||
try { | ||
// `require` will raise an error if this className isn't found: | ||
component = fromCtx(className) | ||
} catch (err) { | ||
// fallback to global: | ||
component = fromGlobal(className) | ||
} | ||
@@ -207,5 +203,5 @@ return component | ||
var detectEvents = __webpack_require__(0) | ||
var constructorFromGlobal = __webpack_require__(1) | ||
var constructorFromRequireContext = __webpack_require__(2) | ||
var detectEvents = __webpack_require__(1) | ||
var constructorFromGlobal = __webpack_require__(0) | ||
var constructorFromRequireContextWithGlobalFallback = __webpack_require__(2) | ||
@@ -261,4 +257,4 @@ var ReactRailsUJS = { | ||
loadContext: function(req) { | ||
this.getConstructor = constructorFromRequireContext(req) | ||
useContext: function(req) { | ||
this.getConstructor = constructorFromRequireContextWithGlobalFallback(req) | ||
}, | ||
@@ -367,4 +363,5 @@ | ||
setup: function(ujs) { | ||
ujs.handleEvent('turbolinks:load', function() { ujs.mountComponents() }); | ||
ujs.handleEvent('turbolinks:before-render', function() { ujs.unmountComponents() }); | ||
ujs.handleEvent('DOMContentLoaded', function() { ujs.mountComponents() }) | ||
ujs.handleEvent('turbolinks:render', function() { ujs.mountComponents() }) | ||
ujs.handleEvent('turbolinks:before-render', function() { ujs.unmountComponents() }) | ||
}, | ||
@@ -405,4 +402,34 @@ } | ||
/***/ }), | ||
/* 12 */ | ||
/***/ (function(module, exports) { | ||
// Load React components by requiring them from "components/", for example: | ||
// | ||
// - "pages/index" -> `require("components/pages/index")` | ||
// - "pages/show.Header" -> `require("components/pages/show").Header` | ||
// - "pages/show.Body.Content" -> `require("components/pages/show").Body.Content` | ||
// | ||
module.exports = function(reqctx) { | ||
return function(className) { | ||
var parts = className.split(".") | ||
var filename = parts.shift() | ||
var keys = parts | ||
// Load the module: | ||
var component = reqctx("./" + filename) | ||
// Then access each key: | ||
keys.forEach(function(k) { | ||
component = component[k] | ||
}) | ||
// support `export default` | ||
if (component.__esModule) { | ||
component = component["default"] | ||
} | ||
return component | ||
} | ||
} | ||
/***/ }) | ||
/******/ ]); | ||
}); |
@@ -7,3 +7,3 @@ var React = require("react") | ||
var constructorFromGlobal = require("./src/getConstructor/fromGlobal") | ||
var constructorFromRequireContext = require("./src/getConstructor/fromRequireContext") | ||
var constructorFromRequireContextWithGlobalFallback = require("./src/getConstructor/fromRequireContextWithGlobalFallback") | ||
@@ -59,4 +59,7 @@ var ReactRailsUJS = { | ||
loadContext: function(req) { | ||
this.getConstructor = constructorFromRequireContext(req) | ||
// Given a Webpack `require.context`, | ||
// try finding components with `require`, | ||
// then falling back to global lookup. | ||
useContext: function(requireContext) { | ||
this.getConstructor = constructorFromRequireContextWithGlobalFallback(requireContext) | ||
}, | ||
@@ -63,0 +66,0 @@ |
{ | ||
"name": "react_ujs", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Rails UJS for the react-rails gem", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# react-rails UJS | ||
UJS driver for [`react-rails`](https://github.com/reactjs/react-rails). | ||
UJS driver for [`react-rails`](https://github.com/reactjs/react-rails). See the Ruby gem for license, documentation and changelog. |
module.exports = { | ||
// Turbolinks 5+ got rid of named events (?!) | ||
setup: function(ujs) { | ||
ujs.handleEvent('turbolinks:load', function() { ujs.mountComponents() }); | ||
ujs.handleEvent('turbolinks:before-render', function() { ujs.unmountComponents() }); | ||
ujs.handleEvent('DOMContentLoaded', function() { ujs.mountComponents() }) | ||
ujs.handleEvent('turbolinks:render', function() { ujs.mountComponents() }) | ||
ujs.handleEvent('turbolinks:before-render', function() { ujs.unmountComponents() }) | ||
}, | ||
} |
82993
15
645