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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
/*global angular */
/**
* Services that persists and retrieves todos from memory or a backend API
* if available.
*
* They both follow the same API, returning promises for all changes to the
* model.
*/
angular.module('todomvc')
.factory('todoStorage', function ($http, $injector) {
'use strict';
return Promise.resolve().then(function() {
return $injector.get('localCache');
});
})
.factory('localCache', function ($q) {
'use strict';
var STORAGE_ID = 'todos-angularjs';
var store = {
todos: [],
cache: [],
_getFromLocalCache: function () {
// return JSON.parse(this.cache[STORAGE_ID] || '[]');
return [];
},
_saveToLocalCache: function (todos) {
// this.cache[STORAGE_ID] = JSON.stringify(todos);
return;
},
clearCompleted: function () {
var deferred = $q.defer();
var incompleteTodos = store.todos.filter(function (todo) {
return !todo.completed;
});
angular.copy(incompleteTodos, store.todos);
store._saveToLocalCache(store.todos);
deferred.resolve(store.todos);
return deferred.promise;
},
delete: function (todo) {
var deferred = $q.defer();
store.todos.splice(store.todos.indexOf(todo), 1);
store._saveToLocalCache(store.todos);
deferred.resolve(store.todos);
return deferred.promise;
},
get: function () {
var deferred = $q.defer();
angular.copy(store._getFromLocalCache(), store.todos);
deferred.resolve(store.todos);
return deferred.promise;
},
insert: function (todo) {
var deferred = $q.defer();
store.todos.push(todo);
store._saveToLocalCache(store.todos);
deferred.resolve(store.todos);
return deferred.promise;
},
put: function (todo, index) {
var deferred = $q.defer();
store.todos[index] = todo;
store._saveToLocalCache(store.todos);
deferred.resolve(store.todos);
return deferred.promise;
}
};
return store;
});
|