-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtodo.js
64 lines (57 loc) · 1.68 KB
/
todo.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
60
61
62
63
64
'use strict';
app.controller('todoController', function($rootScope, $scope) {
var default_todoList = [{
title: 'hello world',
completed: false,
date: new Date().toJSON()
}];
var todo_list_json = localStorage.getItem("todo_list");
if (!todo_list_json) {
$scope.todoList = default_todoList;
} else {
$scope.todoList = JSON.parse(todo_list_json);
}
$scope.saveTodoItems = function(){
localStorage.setItem("todo_list", JSON.stringify($scope.todoList));
};
$scope.addTodo = function(){
$scope.todoList.push({
title: $scope.newTodoText,
completed: false,
date: new Date().toJSON()
});
$scope.newTodoText = '';
$scope.saveTodoItems();
};
$scope.$on('update-todo', function(event, args) {
switch (args.update_type){
case 'remove':
var item_index = $scope.todoList.indexOf(args.item);
if (item_index > -1) {
$scope.todoList.splice(item_index, 1);
}
break;
}
$scope.saveTodoItems();
});
$scope.removeTodo = function($index) {
$scope.todoList.splice($index, 1);
};
});
app.directive('todoItem', function($rootScope){
return {
restrict: 'E',
templateUrl: 'todo-item.html',
scope: {
todoData: '='
},
link: function(scope, element){
scope.removeTodo = function(item){
$rootScope.$broadcast('update-todo', {
item: item,
update_type: 'remove'
});
};
}
};
});