-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
70 lines (49 loc) · 1.67 KB
/
app.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
65
66
67
68
69
70
(function (angular) {
var app = angular.module('taskApp', []);
app.controller('TaskController', function ($scope, StorageService) {
$scope.tasks = ["one","two"];
$scope.addTask = function (task) {
$scope.tasks.push(task);
$scope.$emit('task:added', 'task just added!');
backupTaskList();
refreshTasks();
};
$scope.removeTask = function (task) {
var i = $scope.tasks.indexOf(task);
$scope.tasks.splice(i, 1);
$scope.$emit('task:removed');
backupTaskList();
refreshTasks();
};
function refreshTasks() {
$scope.tasks = StorageService.getData('tasks');
}
function backupTaskList() {
StorageService.save('tasks', $scope.tasks);
}
});
app.service('StorageService', function ($http) {
this.save = function (key, value) {
localStorage.setItem(key, JSON.stringify(value));
$http.post('/tasks', {key: value});
};
this.getData = function (key) {
return JSON.parse(localStorage.getItem(key));
};
this.clear = function () {
localStorage.clear();
};
});
app.directive('nkAlert', function () {
return {
template: "<span>{{alert.msg}}</span>",
replace: true,
link: function (scope, elem, attrs) {
scope.$on('task:added', function (data) {
attrs.$addClass(data.name.split(':').join('-'));
scope.alert = {msg: data.name};
})
}
}
});
}(angular));