forked from tgaff/listly_rails_angular
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lists.controller.js
55 lines (47 loc) · 1.54 KB
/
lists.controller.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
angular
.module('ListlyApp')
.controller('ListsController', ListsController);
ListsController.$inject = ['ListsService', '$location'];
function ListsController( ListsService, $location ) {
var vm = this;
console.log('ListsController is live');
vm.lists = [];
vm.newListName = '';
vm.deleteList = deleteList;
vm.createList = createList;
vm.showList = showList;
// fetch data
getLists();
function getLists() {
ListsService.query(function(data){
console.log('here\'s the lists data in the controller', data);
vm.lists = data;
});
}
function deleteList(list, $event) {
ListsService.remove({id: list.id}, handleDeleteSuccess);
// we can get access to the 'click' or other event using $event (see the template also)
// we don't want to trigger both deleteList and showList so we stop event propagation
$event.stopPropagation();
// declaring this inside deleteList to have a closure around list variable
function handleDeleteSuccess(data) {
console.log('deleted');
vm.lists.splice(vm.lists.indexOf(list), 1);
}
}
function createList() {
console.log('create with', vm.newListName);
if(vm.newListName.length > 1) {
ListsService.save({name: vm.newListName}, handleCreateSuccess);
vm.newListName = '';
}
}
function handleCreateSuccess(data) {
console.log('created', data);
vm.lists.unshift(data);
}
function showList(list) {
console.log('transition to showing list:', list);
$location.path('/lists/' + list.id);
}
}