fwebc.js

Toy framework for learning web components
git clone git://git.finwo.net/lib/fwebc.js
Log | Files | Refs | README

commit 64c8901abce610c8f458d41d3da4313dfa27231b
parent cf77fd53db0f1d8ee7be903de57d023a568cb0b5
Author: finwo <finwo@pm.me>
Date:   Wed, 28 Oct 2020 19:20:36 +0100

Use the custom element as this instead of shadow root

Diffstat:
Mindex.js | 20++++++++++++++------
1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/index.js b/index.js @@ -47,26 +47,34 @@ if (template.content.firstChild instanceof HTMLTemplateElement) { template = template.content.firstChild; } - const shadow = this.attachShadow({ mode: 'open' }); - shadow.appendChild(template.content); + this.root = this.attachShadow({ mode: 'open' }); + this.root.appendChild(template.content); // Run plugins for (const plugin of plugins) { - plugin(shadow); + plugin(this); } // Run component code - (new Function([...shadow.children] + (new Function([...this.root.children] .filter(el => el instanceof HTMLScriptElement) .map(el => el.innerHTML) .join('') - )).call(shadow); + )).call(this); // Load dependencies - if (shadow.dependencies) { + if (this.dependencies) { dependencies.forEach(fwebc.load); } } + + update(data) { + Object.entries(data) + .forEach(([key, value]) => { + this.state[key] = this.isObject(this.state[key]) && + this.isObject(value) ? {...this.state[key], ...value} : value; + }); + } }); };