-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
56 lines (44 loc) · 1.28 KB
/
index.html
File metadata and controls
56 lines (44 loc) · 1.28 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Contact API Client</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.11/handlebars.js"></script>
</head>
<body>
<h1>Contact</h1>
<div id="contacts">
</div>
</body>
<script id="entry-template" type="text/x-handlebars-template">
{{#each contacts as |contact|}}
<h2>{{contact.attributes.name}}</h2>
<img src="https://avatars.io/twitter/{{contact.attributes.twitter}}"/>
{{/each}}
</script>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
loadContacts('https://ca-address-book.herokuapp.com/api/contacts')
.then(function(response) {
console.log(response);
renderContacts(response)
})
})
function loadContacts(url) {
return new Promise(function(resolve) {
fetch(url)
.then(function(response) {
resolve(response.json())
})
})
}
function renderContacts(contacts) {
var context = {contacts: contacts.data}
var source = document.getElementById("entry-template").innerHTML;
var template = Handlebars.compile(source);
var html = template(context)
var element = document.getElementById('contacts')
element.innerHTML += html
}
</script>
</html>