Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unmountComponents #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions preact_ujs/dist/preact_ujs.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ return /******/ (function(modules) { // webpackBootstrap
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

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 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?");
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 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 unmountComponents: function(s) {\n var nodes = PreactRailsUJS.findDOMNodes(s)\n for (var i = 0; i < nodes.length; ++i) {\n var node = nodes[i]\n preact.render(null, 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?");

/***/ }),

Expand Down Expand Up @@ -152,4 +152,4 @@ eval("module.exports = __WEBPACK_EXTERNAL_MODULE_preact__;\n\n//# sourceURL=webp
/***/ })

/******/ });
});
});
8 changes: 8 additions & 0 deletions preact_ujs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ var PreactRailsUJS = {
}
},

unmountComponents: function(searchSelector) {
var nodes = PreactRailsUJS.findDOMNodes(searchSelector)
for (var i = 0; i < nodes.length; ++i) {
var node = nodes[i]
preact.render(null, node)
}
},

clearChildNodes: function(node) {
while (node.firstChild) {
node.removeChild(node.firstChild);
Expand Down
Loading