Created
January 27, 2020 20:51
-
-
Save mccallofthewild/2d1ae286618496d74d8259f80c851c21 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| function decodeHtml(html) { | |
| var txt = document.createElement('textarea'); | |
| txt.innerHTML = html; | |
| return txt.value; | |
| } | |
| let renderTemplateElements = (state: State) => { | |
| const stateful = document.querySelectorAll('[stateful]'); | |
| const elementsToTemplates = new Map<Element, string>(); | |
| function renderer(state: State) { | |
| stateful.forEach(el => { | |
| const template = elementsToTemplates.get(el) || decodeHtml(el.innerHTML); | |
| elementsToTemplates.set(el, template); | |
| try { | |
| console.log(template); | |
| el.innerHTML = | |
| new Function( | |
| `console.log(arguments); var state = arguments[0]; return ${template}` | |
| )(state) || ''; | |
| } catch (e) { | |
| el.innerHTML = ''; | |
| console.error(e); | |
| } | |
| }); | |
| } | |
| renderTemplateElements = renderer; | |
| return renderer(state); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment