-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
53 lines (45 loc) · 1.56 KB
/
controller.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
/* Angular controller JS */
function TodoCtrl($scope) {
$scope.todos = [];
var Todo = $data.define("Todo", {
task: String,
done: Boolean,
tsmark: String
});
Todo.readAll().then(function (todos) {
$scope.$apply(function () {
$scope.todos = todos;
});
});
$scope.$watch('search', function(search) {
var promise = (search ? Todo.query('it.task.contains(p)',{p: search}) : Todo.readAll());
promise.then(function (todos) {
$scope.$apply(function () {
$scope.todos = todos;
});
});
});
$scope.remove = function (todo) {
todo.remove()
.then(function() {
$scope.$apply(function() {
var todos = $scope.todos;
todos.splice(todos.indexOf(todo), 1);
});
})
.fail(function(err) {
alert("Error deleting item");
});
}
$scope.addNew = function (todo) {
// set timestamp
var tsmark = Date.now(); //new Date().getTime(); // to IE8 and earlier
$scope.newTodo.tsmark = tsmark;
Todo.save(todo).then(function (todo) {
$scope.$apply(function () {
$scope.newTodo = null;
$scope.todos.push(todo);
});
});
};
}