-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto-do-list.js
60 lines (49 loc) · 1.29 KB
/
to-do-list.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
// modulos:
// * ngResource - API
var app = angular.module("ToDoList", ['LocalStorageModule']);
app.service('ToDoService', function(localStorageService){
this.key = 'angular-todolist';
if(localStorageService.get(this.key)){
this.activities = localStorageService.get(this.key);
} else {
this.activities= [];
}
this.add = function(newActv){
this.activities.push(newActv);
this.updateLocalStorage();
// self or this ??
}
this.updateLocalStorage = function(){
localStorageService.set(this.key, this.activities);
}
this.clean = function(){
this.activities = [];
this.updateLocalStorage();
return this.getAll();
}
this.getAll = function(){
return this.activities;
}
this.removeItem = function(item){
this.activities = this.activities.filter(function(activity){
return activity !== item;
});
this.updateLocalStorage();
return this.getAll();
}
// buscar por qué watch no es recomendable
});
app.controller("ToDoController", function($scope, ToDoService){
$scope.todo = ToDoService.getAll();
$scope.newActv = {};
$scope.addActv = function(){
ToDoService.add($scope.newActv);
$scope.newActv = {};
};
$scope.removeActv = function(item){
$scope.todo = ToDoService.removeItem(item);
};
$scope.clean = function(){
$scope.todo = ToDoService.clean();
}
});