-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.js
More file actions
71 lines (62 loc) · 1.74 KB
/
store.js
File metadata and controls
71 lines (62 loc) · 1.74 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
var items = []
var notifyComponents = function() {
$(ListStore).trigger('storeHasChanged')
}
var findItemById = function(id) {
return items.filter(function(item) {
return item.id === id
})[0]
},
ListStore = {
getItems: function() {
return items
},
loadItems: function() {
var loadRequest = $.ajax({
type: 'GET',
url: "https://listalous.herokuapp.com/lists/stevechendc/"
})
loadRequest.done(function(dataFromServer) {
items= dataFromServer.items
notifyComponents()
})
},
addItem: function(itemDescription) {
var creationRequest = $.ajax({
type: 'POST',
url: "http://listalous.herokuapp.com/lists/stevechendc/items",
data: { description: itemDescription, completed: false }
})
creationRequest.done(function(itemDataFromServer) {
items.push(itemDataFromServer)
notifyComponents()
})
},
toggleCompleteness: function(itemId) {
var item = findItemById(itemId)
var currentCompletedValue = item.completed
var updateRequest = $.ajax({
type: 'PUT',
url: "https://listalous.herokuapp.com/lists/stevechendc/items/" + itemId,
data: { completed: !currentCompletedValue }
})
updateRequest.done(function(itemData) {
item.completed = itemData.completed
notifyComponents()
})
},
removeItem: function(itemId) {
var item = findItemById(itemId)
var currentDeletedValue = item.deleted
var deleteRequest = $.ajax({
type: 'DELETE',
url: "https://listalous.herokuapp.com/lists/stevechendc/items/" + itemId,
data: { deleted: !currentDeletedValue }
})
deleteRequest.done(function(itemData) {
item.deleted = itemData.deleted
location.reload();
notifyComponents()
})
}
}