-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathmain.js
More file actions
73 lines (70 loc) · 2.03 KB
/
main.js
File metadata and controls
73 lines (70 loc) · 2.03 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
68
69
70
71
72
73
const app = new Vue({
el: "#app",
data: {
editFriend: null,
friendname: "",
newFriend: false,
friends: [],
},
methods: {
addFriend(friend) {
this.friends.push({'name' : friend});
this.friendname = "";
this.newFriend = false;
fetch("http://rest.learncode.academy/api/vue-5/friends/", {
body: JSON.stringify({'name' : friend}),
method: "POST",
headers: {
"Content-Type": "application/json",
},
})
},
deleteFriend(id, i) {
fetch("http://rest.learncode.academy/api/vue-5/friends/" + id, {
method: "DELETE"
})
.then(() => {
this.friends.splice(i, 1);
})
},
updateFriend(friend) {
fetch("http://rest.learncode.academy/api/vue-5/friends/" + friend.id, {
body: JSON.stringify(friend),
method: "PUT",
headers: {
"Content-Type": "application/json",
},
})
.then(() => {
this.editFriend = null;
})
}
},
mounted() {
fetch("http://rest.learncode.academy/api/vue-5/friends")
.then(response => response.json())
.then((data) => {
this.friends = data;
})
},
template: `
<div>
<li v-for="friend, i in friends">
<div v-if="editFriend === friend.id">
<input v-on:keyup.13="updateFriend(friend)" v-model="friend.name" />
<button v-on:click="updateFriend(friend)">save</button>
</div>
<div v-else>
<button v-on:click="editFriend = friend.id">edit</button>
<button v-on:click="deleteFriend(friend.id, i)">x</button>
{{friend.name}}
</div>
</li>
<div><button v-on:click="newFriend = true">Add</button></div>
<div v-if="newFriend === true">
<input v-model="friendname" v-on:keyup.13="addFriend(friendname)" />
<button v-on:click="addFriend(friendname)">save</button>
</div>
</div>
`,
});