forked from chandlerprall/DatabindExtension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-manual.html
83 lines (66 loc) · 2.55 KB
/
index-manual.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
html {
box-sizing: border-box;
background-color: white;
}
*, *:before, *:after {
box-sizing: inherit;
}
body {
width: 800px;
margin: 0 auto;
padding: 10px;
background-color: rgb(255, 255, 220);
}
</style>
<script type="text/javascript" src="./js/nunjucks.min.js"></script>
<script type="text/javascript" src="DatabindExtension.js"></script>
</head>
<body>
<input type="text" value="person" id="nameSrc"/>
<div id="container"></div>
<br/>
<button id="refresh">Refresh Data</button>
<script type="text/javascript">
window.addEventListener('load', function () {
// Create Nunjucks environment with the databinding extension
var nunjucksEnv = new nunjucks.Environment(new nunjucks.WebLoader('./templates'), {
autoescape: true
});
var databindExtension = new DatabindExtension({updateMode: 'manual'});
nunjucksEnv.addExtension('BindExtension', databindExtension);
// Object used for rendering templates
var data = {
displayName: 'person',
items: ['Test1', 'Test2']
};
// Fetch the template & render
var template = nunjucksEnv.getTemplate('pageTemplate.njk');
template.render(data, function (error, result) {
var containerDomElement = document.getElementById('container');
containerDomElement.innerHTML = result;
var nameSrcDomElement = document.getElementById('nameSrc');
var addItemDomElement = document.getElementById('add-item');
var newItemDomElement = document.getElementById('newItem');
var refreshDomElement = document.getElementById('refresh');
// Update the displayName value when user types in the nameSrc field
nameSrcDomElement.addEventListener('keyup', function () {
data.displayName = this.value;
});
// Event delegation for adding items to the list
addItemDomElement.addEventListener('click', function (e) {
e.preventDefault();
data.items.push(newItemDomElement.value);
});
refreshDomElement.addEventListener('click', function (e) {
e.preventDefault();
databindExtension.updateBindings();
});
});
});
</script>
</body>
</html>