Using JavaScript Classes from the Parent Page of a Web Component
I'm building a web component that needs to load functionality dynamically from its parent page.
I'm doing it by creating a global variable named "customClasses" in a regular tag in the element on the page1. Class definitions with the necessary functionality are placed inside it.
HTML
Output
I'm using a data-* attribute to send in the name of the class I want to use from the instance of the component.
HTML
waiting...
The web component grabs the name from the dataset and uses it to create an internal instance of the desired class via the "customClasses" object. It can then use it as necessary.
customElements.;
Everything is working as far as I can tell.
HTML
waiting...
Output
But, this feels like one of those things where there might be hidden gotchas or show-stoppers I haven't learned about yet. Please give me a heads up if there's something I should know about.
-a
Endnotes
The example is simplified as much as I know how. Part of the simplification involved having the component update its own text by jumping through the instance of the external class.
That specific update would be easier to do directly inside the component. But, it's not the point of the example. It's merely acting as a stand in for more complicated functionality that would only make the example harder to follow.
Footnotes
Specifically, it's a tag and not a tag.