-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcc-smartphone-shop.js
64 lines (56 loc) · 2.01 KB
/
cc-smartphone-shop.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class CcSmartphoneShop extends HTMLElement {
constructor() {
super();
// DOM elements
this._root = this.attachShadow({ mode: 'open' });
// data
this._smartphones = [
{
id: 1,
brand: 'Samsung',
model: 'Galaxy S7 Edge',
description: 'Le smartphone Honor 9, nouveau très haut de gamme Honor, bla bla'
},
{
id: 2,
brand: 'Honor',
model: '9 (bleu)',
description: 'Nouveau fer de lance de la marque, bli bli'
}
];
}
connectedCallback() {
this._root.innerHTML = `
<style>
.frame {
border: 2px dotted grey;
margin-bottom: 10px;
padding-left: 10px;
}
h1, h2 {
color: green;
}
</style>
<template id="smartphone-template">
<div class="frame">
<h2 id="brand"></h2>
<p id="model"></p>
<p id="description"></p>
</div>
</template>
<div id="result"></div>
`;
this._templateContent = this._root.querySelector('#smartphone-template').content;
this._result = this._root.querySelector('#result');
this._smartphones.map(smartphone => {
const clone = document.importNode(this._templateContent, true);
// update the DOM with current smartphone data
clone.querySelector('#brand').innerHTML = smartphone.brand;
clone.querySelector('#model').innerHTML = smartphone.model;
clone.querySelector('#description').innerHTML = smartphone.description;
// add to the DOM
this._result.appendChild(clone);
});
}
} // end class
window.customElements.define('cc-smartphone-shop', CcSmartphoneShop);