-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsimple-list.html
49 lines (44 loc) · 1.41 KB
/
simple-list.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
46
47
48
49
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example: simple list</title>
</head>
<body>
<!-- target element -->
<div id="container"></div>
<!-- templates -->
<template class="magery-templates">
<div data-template="main" id="container">
<ul>
<li data-each="item in app.items">
{{item.name}} <button onclick="removeItem(app, item)">X</button>
</li>
</ul>
<button onclick="addItem(app)">add item</button>
</div>
</template>
<script src="../build/magery.js"></script>
<script>
var templates = Magery.compile('.magery-templates');
var element = document.getElementById('container');
var data = {app: {items: []}};
function patch() {
Magery.patch(templates, 'main', data, element);
}
templates['main'].bind({
addItem: function (app) {
app.items.push({name: '' + Math.random()});
patch();
},
removeItem: function (app, item) {
app.items = app.items.filter(function (x) {
return x !== item;
});
patch();
}
});
patch();
</script>
</body>
</html>