-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathdata_handler.js
More file actions
79 lines (75 loc) · 3.5 KB
/
data_handler.js
File metadata and controls
79 lines (75 loc) · 3.5 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
74
75
76
77
78
79
// this object contains the functions which handle the data and its reading/writing
// feel free to extend and change to fit your needs
// (watch out: when you would like to use a property/function of an object from the
// object itself then you must use the 'this' keyword before. For example: 'this._data' below)
export let dataHandler = {
_data: {}, // it contains the boards and their cards and statuses. It is not called from outside.
_api_get: function (url, callback) {
// it is not called from outside
// loads data from API, parses it and calls the callback with it
const json = JSON.parse('[{"id": "1", "title": "Board 1"}, {"id": "2", "title": "Board 2"}]');
fetch(url, {
method: 'GET',
credentials: 'same-origin'
})
.then(response => response.json()) // parse the response as JSON
.then(json_response => callback(json_response)); // Call the `callback` with the returned object
},
_api_post: function (url, data, callback) {
// it is not called from outside
// sends the data to the API, and calls callback function
fetch(url, {
method: 'POST',
credentials: 'same-origin',
headers: {
"Content-Type": "application/json",
// "Content-Type": "application/x-www-form-urlencoded",
},
body: JSON.stringify(data)
})
.then(response => response.json()) // parse the response as JSON
.then(json_response => callback(json_response));
},
init: function () {
},
getBoards: function (callback) {
// the boards are retrieved and then the callback function is called with the boards
// Here we use an arrow function to keep the value of 'this' on dataHandler.
// if we would use function(){...} here, the value of 'this' would change.
this._api_get('/get-boards', (response) => {
this._data = response;
callback(response);
});
},
addNewBoard: function (callback) {
// the boards are retrieved and then the callback function is called with the boards
// Here we use an arrow function to keep the value of 'this' on dataHandler.
// if we would use function(){...} here, the value of 'this' would change.
this._api_post('/add-new-board', {title: 'New Board'}, (response) => {
this._data = response;
callback(response);
});
},
getBoard: function (boardId, callback) {
// the board is retrieved and then the callback function is called with the board
},
getStatuses: function (callback) {
// the statuses are retrieved and then the callback function is called with the statuses
},
getStatus: function (statusId, callback) {
// the status is retrieved and then the callback function is called with the status
},
getCardsByBoardId: function (boardId, callback) {
// the cards are retrieved and then the callback function is called with the cards
},
getCard: function (cardId, callback) {
// the card is retrieved and then the callback function is called with the card
},
createNewBoard: function (boardTitle, callback) {
// creates new board, saves it and calls the callback function with its data
},
createNewCard: function (cardTitle, boardId, statusId, callback) {
// creates new card, saves it and calls the callback function with its data
}
// here comes more features
};