-
Notifications
You must be signed in to change notification settings - Fork 0
/
x-counter-0.html
45 lines (34 loc) · 928 Bytes
/
x-counter-0.html
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
45
<template id="tpl">
<style>
span {
color: green;
font-weight: bold;
}
</style>
<span> click me! </span>
</template>
<script>
var proto = Object.create(HTMLElement.prototype);
Object.defineProperty(proto, "count", {
value: 0,
writable: true
});
proto.inc = function () {
this.count += 1;
this.shadowRoot.querySelector('span').textContent = this.count;
}
var importee = document.currentScript.ownerDocument;
proto.createdCallback = function () {
var tpl = importee.querySelector('#tpl');
this.createShadowRoot();
this.shadowRoot.appendChild(tpl.content.cloneNode(true));
this.addEventListener('click', this.inc.bind(this));
}
proto.attributeChangedCallback = function (attr, oldVal, newVal) {
if (attr === 'count') {
this.count = parseInt(newVal);
}
}
document.registerElement('x-counter-0', { prototype: proto });
</script>
<x-counter-0 count="10">Hello</x-counter-0>