Skip to content

Instantly share code, notes, and snippets.

@matthewp
Created June 12, 2019 12:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewp/6830c5997a81a0020103cb28f5164c71 to your computer and use it in GitHub Desktop.
Save matthewp/6830c5997a81a0020103cb28f5164c71 to your computer and use it in GitHub Desktop.
Observed attributes dynamic
<!doctype html>
<html lang="en">
<title>Some title</title>
<my-element id="myel"><my-element>
<script type="module">
class MyElement extends HTMLElement {
static get observedAttributes() {
let defines = this.define;
let attrs = [];
for(let [prop, definition] of Object.entries(defines)) {
if(definition.attribute) {
attrs.push(prop);
}
}
return attrs;
}
static get define() {
return {
foo: {
attribute: true
}
}
}
attributeChangedCallback(attrName, oldValue, newValue) {
console.log(attrName, 'changed to', newValue);
}
}
customElements.define('my-element', MyElement);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment