Created
October 5, 2018 05:23
-
-
Save phoenisx/821379521d672b6edf779078e88ecafd to your computer and use it in GitHub Desktop.
Some Useful Compose Functions for day-to-day uses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A DOM Node Caching Compose Function, that can be used in a Classes for Old Legacy Codes | |
* that didn't use any UI Frameworks, like React | |
* | |
* @param {HTMLElement} ref - A parent reference that will be used to query, to fetch inner nodes | |
* DEFAULT: `document` | |
*/ | |
nodeCache = (ref = document) => { | |
const node = ref; | |
const cache = {}; | |
return (key) => { | |
if (!cache.hasOwnProperty(key)) { | |
const childNode = node.querySelectorAll(key); | |
if (childNode.length) cache[key] = childNode; | |
} | |
return cache[key] || []; | |
}; | |
}; | |
/** | |
* Usage Example: | |
* | |
* constructor(options) { | |
* this.nodeCache = nodeCache(options.node); | |
* this.nodeCache('.className'); | |
* this.nodeCache('#idName'); | |
* } | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment