-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathapp.js
More file actions
67 lines (59 loc) · 1.66 KB
/
app.js
File metadata and controls
67 lines (59 loc) · 1.66 KB
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
65
66
67
'use strict'
module.exports = function () {
// Allow `bel` to be swapped in benchmarking
const html = require('bel')
const greeting = 'Hello'
const name = 'special characters, <, >, &'
const drinks = [
{ name: 'Cafe Latte', price: 3.0, sold: false },
{ name: 'Cappucino', price: 2.9, sold: true },
{ name: 'Club Mate', price: 2.2, sold: true },
{ name: 'Berliner Weiße', price: 3.5, sold: false }
]
const listeners = []
function onChange (listener) {
listeners.push(listener)
}
function notifyChange () {
listeners.forEach((listener) => listener())
}
function deleteDrink (drink) {
const index = drinks.indexOf(drink)
if (index >= 0) {
drinks.splice(index, 1)
}
notifyChange()
}
function drinkView (drink, deleteDrink) {
return html`
<li>
${drink.name} is € ${drink.price}
<button ${{type: 'submit', 'data-ga-btn': 'Button'}} onclick=${() => deleteDrink(drink)} disabled="${!drink.sold}">Give me!</button>
</li>
`
}
function mainView (greeting, name, drinks, deleteDrink) {
return html`
<div>
<p>${greeting}, ${name}!</p>
${drinks.length > 0 ? html`
<ul>
${drinks.map(drink => drinkView(drink, deleteDrink))}
</ul>
` : html`
<p>All drinks are gone!</p>
`}
<p>
attributes: <input type=text value=${''} disabled onclick="${() => alert('hello')}" ${{ title: '<Special " characters>' }} />
</p>
</div>
`
}
function render () {
return mainView(greeting, name, drinks, deleteDrink)
}
return {
render: render,
onChange: onChange
}
}