-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
62 lines (51 loc) · 2.06 KB
/
demo.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
// We use the Objects below to control toggling like / unlike status
const glyphStates = {
"♡": "♥",
"♥": "♡"
};
const colorStates = {
"red" : "",
"": "red"
};
// STEP 1: The line of code below is what lets JavaScript find the elements that
// we want to make clickable. Without JavaScript, clicking on these heart shapes
// does nothing. Uncomment the code and refresh the demo page.
const articleHearts = document.querySelectorAll(".like-glyph");
function likeCallback(e) {
const heart = e.target;
mimicServerCall()
.then(function(serverMessage){
// STEP 2: Uncomment the 3 lines after the alert.
// Here we're using Pillar 1 (DOM Manipulation) to update the screen and
// mimicking Pillar 3 (Server Communication) to only update the screen if
// the sending of information to the server succeeds.
alert("You notified the server!");
alert(serverMessage);
heart.innerText = glyphStates[heart.innerText];
heart.style.color = colorStates[heart.style.color];
})
.catch(function(error) {
alert("Something went wrong!");
});
}
// STEP 3: In order for the call to the server and the update of the screen to
// work, we need to add a click event listener to the elements we identified in
// STEP 1. That's Pillar 2, event handling. Uncomment this code:
for (const glyph of articleHearts) {
glyph.addEventListener("click", likeCallback);
}
// STEP 4:
// When all the STEPs' code changes have been complete, you'll be able to see a
// working demonstration of our reference example. Sure, it's maybe not as
// pretty as a professional site, but they're only different from our site in
// style, not substance.
//------------------------------------------------------------------------------
// Ignore after this point. Used only for demo purposes
//------------------------------------------------------------------------------
function mimicServerCall() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve("Pretend remote server notified of action!");
}, 300);
});
}