|
1 | | -(function(){ |
| 1 | +(function () { |
| 2 | + angular |
| 3 | + .module('app') |
| 4 | + .controller('TodoController', [ |
| 5 | + 'todoListService', |
| 6 | + TodoController |
| 7 | + ]); |
2 | 8 |
|
3 | | - angular |
4 | | - .module('app') |
5 | | - .controller('TodoController', [ |
6 | | - 'todoListService', |
7 | | - TodoController |
8 | | - ]); |
| 9 | + function TodoController(todoListService) { |
| 10 | + var vm = this; |
9 | 11 |
|
10 | | - function TodoController(todoListService) { |
11 | | - var vm = this; |
| 12 | + vm.addTodo = addTodo; |
| 13 | + vm.remaining = remaining; |
| 14 | + vm.archive = archive; |
| 15 | + vm.toggleAll = toggleAll; |
| 16 | + vm.todos = []; |
12 | 17 |
|
13 | | - vm.addTodo = addTodo; |
14 | | - vm.remaining = remaining; |
15 | | - vm.archive = archive; |
16 | | - vm.todos = []; |
| 18 | + todoListService |
| 19 | + .loadAllItems() |
| 20 | + .then(function (todos) { |
| 21 | + vm.todos = [].concat(todos); |
| 22 | + }); |
17 | 23 |
|
18 | | - todoListService |
19 | | - .loadAllItems() |
20 | | - .then(function(todos) { |
21 | | - vm.todos = [].concat(todos); |
22 | | - }); |
| 24 | + function addTodo() { |
| 25 | + vm.todos.push({text: vm.todoText, done: false}); |
| 26 | + vm.todoText = ''; |
| 27 | + } |
23 | 28 |
|
24 | | - function addTodo() { |
25 | | - vm.todos.push({text: vm.todoText, done: false}); |
26 | | - vm.todoText = ''; |
27 | | - } |
| 29 | + function remaining() { |
| 30 | + var count = 0; |
| 31 | + angular.forEach(vm.todos, function (todo) { |
| 32 | + count += todo.done ? 0 : 1; |
| 33 | + }); |
| 34 | + return count; |
| 35 | + } |
28 | 36 |
|
29 | | - function remaining() { |
30 | | - var count = 0; |
31 | | - angular.forEach(vm.todos, function (todo) { |
32 | | - count += todo.done ? 0 : 1; |
33 | | - }); |
34 | | - return count; |
35 | | - } |
| 37 | + function archive(e) { |
| 38 | + // Prevent from submitting |
| 39 | + e.preventDefault(); |
| 40 | + var oldTodos = vm.todos; |
| 41 | + vm.todos = []; |
| 42 | + angular.forEach(oldTodos, function (todo) { |
| 43 | + if (!todo.done) vm.todos.push(todo); |
| 44 | + }); |
| 45 | + } |
36 | 46 |
|
37 | | - function archive(e) { |
38 | | - // Prevent from submitting |
39 | | - e.preventDefault(); |
40 | | - var oldTodos = vm.todos; |
41 | | - vm.todos = []; |
42 | | - angular.forEach(oldTodos, function (todo) { |
43 | | - if (!todo.done) vm.todos.push(todo); |
44 | | - }); |
| 47 | + function toggleAll() { |
| 48 | + if (remaining() == 0) { |
| 49 | + angular.forEach(vm.todos, function (todo) { |
| 50 | + todo.done = false; |
| 51 | + }); |
| 52 | + } else { |
| 53 | + angular.forEach(vm.todos, function (todo) { |
| 54 | + todo.done = true; |
| 55 | + }); |
| 56 | + } |
| 57 | + } |
45 | 58 | } |
46 | | - } |
47 | 59 | })(); |
0 commit comments