refactor(site): update vanilla observed attribute
This commit is contained in:
parent
1c38ec8e1e
commit
2b0f85c2eb
2 changed files with 16 additions and 9 deletions
|
@ -1,20 +1,21 @@
|
|||
class HelloWorld extends HTMLElement {
|
||||
static get observedAttributes() {
|
||||
return ["data-name"];
|
||||
return ["my-name"];
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
let count = 0;
|
||||
const currentName = this.getAttribute('my-name');
|
||||
|
||||
if (!('name' in this.dataset)) {
|
||||
this.dataset.name = 'World'
|
||||
if (!currentName) {
|
||||
this.setAttribute('my-name', 'World')
|
||||
}
|
||||
|
||||
this.onclick = () => this.dataset.name = `Clicked ${++count}x`;
|
||||
this.onclick = () => this.setAttribute("my-name", `Clicked ${++count}x`);
|
||||
}
|
||||
|
||||
attributeChangedCallback(property, previousValue, currentValue) {
|
||||
if (property === "data-name" && previousValue !== currentValue) {
|
||||
if (property === "my-name" && previousValue !== currentValue) {
|
||||
this.innerHTML = `<button style="cursor:pointer">Hello ${currentValue}!</button>`;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,18 +30,24 @@
|
|||
<code-block language="js">
|
||||
class HelloWorld extends HTMLElement {
|
||||
static get observedAttributes() {
|
||||
return ["data-name"];
|
||||
return ["my-name"];
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
let count = 0;
|
||||
const currentName = this.getAttribute("my-name");
|
||||
|
||||
if (!currentName) {
|
||||
this.setAttribute("my-name", "World")
|
||||
}
|
||||
|
||||
this.onclick = () => {
|
||||
this.setAttribute("data-name", `Clicked ${++count}x`);
|
||||
this.setAttribute("my-name", `Clicked ${++count}x`);
|
||||
};
|
||||
}
|
||||
|
||||
attributeChangedCallback(property, previousValue, currentValue) {
|
||||
if (property === "data-name" && previousValue !== currentValue) {
|
||||
if (property === "my-name" && previousValue !== currentValue) {
|
||||
this.innerHTML = `<button style="cursor:pointer">Hello ${currentValue}!</button>`;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue