-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
59 lines (54 loc) · 1.46 KB
/
script.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
'use strict';
// Your client side JavaScript code goes here.
// This file is included in every page.
// Example code for creating a list on the server
function createList(name, pos, cards) {
return $.ajax('/api/lists', {
type: 'POST',
data: {
name: name,
pos: pos,
cards: cards
}
});
}
// Example code for getting all `list`s from server
function loadLists() {
return $.ajax('/api/lists');
}
// Example code for displaying lists in the browser
function displayLists(lists) {
// Lists should be ordered based on their 'pos' field
lists.rows = _.sortBy(lists.rows, 'pos');
lists.rows.forEach(function(list) {
var curElem = $('<li>').text(list.name);
if (list.cards) {
var innerUl = $('<ul>');
list.cards.forEach(function(card) {
innerUl.append($('<li>').text(card));
});
curElem.append(innerUl);
}
$('#lists').append(curElem);
});
}
loadLists()
.then(function(data) {
console.log('Lists', data.rows);
if (data.rows.length) {
// If some lists are found display them
displayLists(data);
} else {
// If no lists are found, create sample list
// and re-display.
console.log('No lists found, creating one.');
createList('Hello', 0, ['Card 1', 'Card 2'])
.then(function(list) {
console.log('Created list', list);
return loadLists();
})
.then(function(lists) {
displayLists(lists);
})
}
});