-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmp.js
44 lines (37 loc) · 860 Bytes
/
tmp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class MyCounter extends HTMLElement {
constructor() {
super();
this.shadow = this.attachShadow({ mode: "open" });
}
get count() {
return this.getAttribute("count");
}
set count(val) {
this.setAttribute("count", val);
}
static get observedAttributes() {
return ["count"];
}
attributeChangedCallback(prop, oldVal, newVal) {
if (prop === "count") {
this.render();
let btn = this.shadow.querySelector("#btn");
btn.addEventListener("click", this.inc.bind(this));
}
}
connectedCallback() {
this.render();
let btn = this.shadow.querySelector("#btn");
btn.addEventListener("click", this.inc.bind(this));
}
inc() {
this.count++;
}
render() {
this.shadow.innerHTML = `
<h1>Counter</h1>
${this.count}
<button id="btn">Increment</button>
`;
}
}