-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.js
41 lines (39 loc) · 1.13 KB
/
services.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
import angular from 'angular';
angular.module('todo').factory('todoStorage', function () {
var TODO_DATA = 'TODO_DATA';
var storage = {
todos: [],
_saveToLocalStorage: function (data) {
localStorage.setItem(TODO_DATA, JSON.stringify(data));
},
_getFromLocalStorage: function () {
return JSON.parse(localStorage.getItem(TODO_DATA)) || [];
},
get: function () {
angular.copy(storage._getFromLocalStorage(), storage.todos);
return storage.todos;
},
remove: function (todo) {
var idx = storage.todos.findIndex(function (item) {
return item.id === todo.id;
});
if (idx > -1) {
storage.todos.splice(idx, 1);
storage._saveToLocalStorage(storage.todos);
}
},
add: function (newTodoTitle) {
var newTodo = {
title: newTodoTitle,
completed: false,
createdAt: Date.now(),
};
storage.todos.push(newTodo);
storage._saveToLocalStorage(storage.todos);
},
update: function(){
storage._saveToLocalStorage(storage.todos);
}
}
return storage;
});