-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.js
More file actions
executable file
·61 lines (55 loc) · 1.52 KB
/
store.js
File metadata and controls
executable file
·61 lines (55 loc) · 1.52 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
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/xiaodianwang/'
})
loadRequest.done(function(dataFromServer) {
items = dataFromServer.items
notifyComponents()
})
},
addItem: function(itemDescription) {
var creationRequest = $.ajax({
type: 'POST',
url: 'http://listalous.herokuapp.com/lists/xiaodianwang/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/xiaodianwang/items/' + itemId,
data: {completed: !currentCompletedValue}
})
updateRequest.done(function (itemData) {
item.completed = itemData.completed
notifyComponents()
})
}
deleteItem: function(itemId) {
var item = findItemById(itemId)
var deleteRequest = $.ajax({
type: 'DELETE',
url: 'http://listalous.herokuapp.com/lists/xiaodianwang/items/' + itemId,
})
}
}