Skip to content

Commit 554b27a

Browse files
committed
Feature: clear child nodes before mounting components
1 parent f9b186a commit 554b27a

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

preact_ujs/dist/preact_ujs.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
(function webpackUniversalModuleDefinition(root, factory) {
22
if(typeof exports === 'object' && typeof module === 'object')
3-
module.exports = factory(require("preact"));
3+
module.exports = factory();
44
else if(typeof define === 'function' && define.amd)
5-
define(["preact"], factory);
5+
define([], factory);
66
else if(typeof exports === 'object')
7-
exports["PreactRailsUJS"] = factory(require("preact"));
7+
exports["PreactRailsUJS"] = factory();
88
else
9-
root["PreactRailsUJS"] = factory(root["Preact"]);
10-
})(window, function(__WEBPACK_EXTERNAL_MODULE_preact__) {
9+
root["PreactRailsUJS"] = factory();
10+
})(window, function() {
1111
return /******/ (function(modules) { // webpackBootstrap
1212
/******/ // The module cache
1313
/******/ var installedModules = {};
@@ -103,7 +103,7 @@ return /******/ (function(modules) { // webpackBootstrap
103103
/*! no static exports found */
104104
/***/ (function(module, exports, __webpack_require__) {
105105

106-
eval("var preact = __webpack_require__(/*! preact */ \"preact\")\nvar constructorFromGlobal = __webpack_require__(/*! ./src/getConstructor/fromGlobal */ \"./src/getConstructor/fromGlobal.js\")\nvar constructorFromRequireContextWithGlobalFallback = __webpack_require__(/*! ./src/getConstructor/fromRequireContextWithGlobalFallback */ \"./src/getConstructor/fromRequireContextWithGlobalFallback.js\")\n\nvar PreactRailsUJS = {\n CLASS_NAME_ATTR: 'data-preact-class',\n PROPS_ATTR: 'data-preact-props',\n\n findDOMNodes: function() {\n return document.querySelectorAll('['+PreactRailsUJS.CLASS_NAME_ATTR+']')\n },\n\n getConstructor: constructorFromGlobal,\n\n useContext: function(requireContext) {\n this.getConstructor = constructorFromRequireContextWithGlobalFallback(requireContext)\n },\n\n mountComponents: function() {\n console.log('mountcomponent loaded')\n var nodes = PreactRailsUJS.findDOMNodes()\n for (var i = 0; i < nodes.length; ++i) {\n var node = nodes[i]\n var className = node.getAttribute(PreactRailsUJS.CLASS_NAME_ATTR)\n var constructor = PreactRailsUJS.getConstructor(className)\n var propsJson = node.getAttribute(PreactRailsUJS.PROPS_ATTR)\n var props = propsJson && JSON.parse(propsJson)\n preact.render(preact.h(constructor, props), node)\n }\n },\n\n handleEvent: function(eventName, callback) {\n document.addEventListener(eventName, callback);\n },\n\n removeEvent: function(eventName, callback) {\n document.removeEventListener(eventName, callback);\n }\n\n}\n\nPreactRailsUJS.handleEvent('DOMContentLoaded', PreactRailsUJS.mountComponents)\n\nmodule.exports = PreactRailsUJS\n\n\n//# sourceURL=webpack://PreactRailsUJS/./index.js?");
106+
eval("//var preact = require(\"preact\")\nvar constructorFromGlobal = __webpack_require__(/*! ./src/getConstructor/fromGlobal */ \"./src/getConstructor/fromGlobal.js\")\nvar constructorFromRequireContextWithGlobalFallback = __webpack_require__(/*! ./src/getConstructor/fromRequireContextWithGlobalFallback */ \"./src/getConstructor/fromRequireContextWithGlobalFallback.js\")\n\nvar PreactRailsUJS = {\n CLASS_NAME_ATTR: 'data-preact-class',\n PROPS_ATTR: 'data-preact-props',\n\n findDOMNodes: function() {\n return document.querySelectorAll('['+PreactRailsUJS.CLASS_NAME_ATTR+']')\n },\n\n getConstructor: constructorFromGlobal,\n\n useContext: function(requireContext) {\n this.getConstructor = constructorFromRequireContextWithGlobalFallback(requireContext)\n },\n\n mountComponents: function() {\n console.log('mountcomponent loaded')\n var nodes = PreactRailsUJS.findDOMNodes()\n for (var i = 0; i < nodes.length; ++i) {\n var node = nodes[i]\n //PreactRailsUJS.clearChildNodes(node)\n var className = node.getAttribute(PreactRailsUJS.CLASS_NAME_ATTR)\n var constructor = PreactRailsUJS.getConstructor(className)\n var propsJson = node.getAttribute(PreactRailsUJS.PROPS_ATTR)\n var props = propsJson && JSON.parse(propsJson)\n preact.render(preact.h(constructor, props), node)\n }\n },\n\n clearChildNodes: function(node) {\n while (node.firstChild) {\n node.removeChild(node.firstChild);\n }\n },\n\n handleEvent: function(eventName, callback) {\n document.addEventListener(eventName, callback);\n },\n\n removeEvent: function(eventName, callback) {\n document.removeEventListener(eventName, callback);\n }\n\n}\n\nPreactRailsUJS.handleEvent('DOMContentLoaded', PreactRailsUJS.mountComponents)\n\nmodule.exports = PreactRailsUJS\n\n\n//# sourceURL=webpack://PreactRailsUJS/./index.js?");
107107

108108
/***/ }),
109109

@@ -138,17 +138,6 @@ eval("// Load React components by requiring them from \"components/\", for examp
138138

139139
eval("// Make a function which:\n// - First tries to require the name\n// - Then falls back to global lookup\nvar fromGlobal = __webpack_require__(/*! ./fromGlobal */ \"./src/getConstructor/fromGlobal.js\")\nvar fromRequireContext = __webpack_require__(/*! ./fromRequireContext */ \"./src/getConstructor/fromRequireContext.js\")\n\nmodule.exports = function(reqctx) {\n var fromCtx = fromRequireContext(reqctx)\n return function(className) {\n var component;\n try {\n // `require` will raise an error if this className isn't found:\n component = fromCtx(className)\n } catch (firstErr) {\n // fallback to global:\n try {\n component = fromGlobal(className)\n } catch (secondErr) {\n console.error(firstErr)\n console.error(secondErr)\n }\n }\n return component\n }\n}\n\n\n//# sourceURL=webpack://PreactRailsUJS/./src/getConstructor/fromRequireContextWithGlobalFallback.js?");
140140

141-
/***/ }),
142-
143-
/***/ "preact":
144-
/*!******************************************************************************************!*\
145-
!*** external {"root":"Preact","commonjs2":"preact","commonjs":"preact","amd":"preact"} ***!
146-
\******************************************************************************************/
147-
/*! no static exports found */
148-
/***/ (function(module, exports) {
149-
150-
eval("module.exports = __WEBPACK_EXTERNAL_MODULE_preact__;\n\n//# sourceURL=webpack://PreactRailsUJS/external_%7B%22root%22:%22Preact%22,%22commonjs2%22:%22preact%22,%22commonjs%22:%22preact%22,%22amd%22:%22preact%22%7D?");
151-
152141
/***/ })
153142

154143
/******/ });

preact_ujs/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var preact = require("preact")
1+
//var preact = require("preact")
22
var constructorFromGlobal = require("./src/getConstructor/fromGlobal")
33
var constructorFromRequireContextWithGlobalFallback = require("./src/getConstructor/fromRequireContextWithGlobalFallback")
44

@@ -17,10 +17,10 @@ var PreactRailsUJS = {
1717
},
1818

1919
mountComponents: function() {
20-
console.log('mountcomponent loaded')
2120
var nodes = PreactRailsUJS.findDOMNodes()
2221
for (var i = 0; i < nodes.length; ++i) {
2322
var node = nodes[i]
23+
PreactRailsUJS.clearChildNodes(node)
2424
var className = node.getAttribute(PreactRailsUJS.CLASS_NAME_ATTR)
2525
var constructor = PreactRailsUJS.getConstructor(className)
2626
var propsJson = node.getAttribute(PreactRailsUJS.PROPS_ATTR)
@@ -29,6 +29,12 @@ var PreactRailsUJS = {
2929
}
3030
},
3131

32+
clearChildNodes: function(node) {
33+
while (node.firstChild) {
34+
node.removeChild(node.firstChild);
35+
}
36+
},
37+
3238
handleEvent: function(eventName, callback) {
3339
document.addEventListener(eventName, callback);
3440
},

preact_ujs/package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

preact_ujs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"author": "",
88
"license": "ISC",
99
"devDependencies": {
10+
"preact": "^8.4.2",
1011
"webpack": "^4.32.2",
1112
"webpack-cli": "^3.3.2"
1213
}

0 commit comments

Comments
 (0)