Permalink
Browse files

Step 12: persist changes to checked status and automatic updating

  • Loading branch information...
vkarpov15 committed Jul 25, 2013
1 parent 0af8465 commit e8fc93e12f68a962ee3dae0eb748e7dc4af62853
Showing with 57 additions and 3 deletions.
  1. +3 −0 app.js
  2. +25 −1 public/javascripts/controllers/TodoListController.js
  3. +27 −0 routes/index.js
  4. +2 −2 views/index.jade
View
3 app.js
@@ -35,6 +35,9 @@ if ('development' == app.get('env')) {
app.get('/', routes.index(Todo));
app.get('/users', user.list);
app.get('/todos.json', routes.get(Todo));
app.put('/todo/:id.json', routes.update(Todo));
app.post('/todo.json', routes.addTodo(Todo));
@@ -1,4 +1,4 @@
function TodoListController($scope, $http) {
function TodoListController($scope, $http, $timeout) {
$scope.todos = [];
$scope.newTodo = {
done : false,
@@ -13,6 +13,30 @@ function TodoListController($scope, $http) {
$scope.todos = todos;
};
$scope.update = function(todo) {
$http.put('/todo/' + todo._id + '.json', todo).success(function(data) {
if (!data.todo) {
alert(JSON.stringify(data));
}
});
};
$scope.updateList = function() {
$http.get('/todos.json').success(function(data) {
$scope.todos = data.todos;
});
$timeout(function() {
$scope.updateList();
}, 30 * 60 * 1000); // update every 30 minutes;
};
$timeout(function() {
$scope.updateList();
}, 30 * 60 * 1000); // update every 30 minutes;
$scope.updateList();
$scope.addNewTodo = function() {
$http.post('/todo.json', $scope.newTodo).success(function(data) {
if (data.todo) {
View
@@ -25,4 +25,31 @@ exports.addTodo = function(Todo) {
}
});
};
};
exports.get = function(Todo) {
return function(req, res) {
Todo.find({}, function(error, todos) {
res.json({ todos : todos });
});
}
};
exports.update = function(Todo) {
return function(req, res) {
Todo.findOne({ _id : req.params.id }, function(error, todo) {
if (error || !todo) {
res.json({ error : error });
} else {
todo.done = req.body.done;
todo.save(function(error, todo) {
if (error || !todo) {
res.json({ error : error });
} else {
res.json({ todo : todo });
}
});
}
});
}
};
View
@@ -21,7 +21,7 @@ block content
div.row.todo.not-done(ng-repeat="todo in todos | filter:notDoneFilter")
div.span9
label.checkbox
input(type="checkbox", ng-model="todo.done")
input(type="checkbox", ng-model="todo.done", ng-change="update(todo)")
| {{todo.description}}
div.span3.text-right
i {{todo.due | date:'MMM d, yyyy'}}
@@ -30,7 +30,7 @@ block content
div.row.todo.done(ng-repeat="todo in todos | filter:doneFilter")
div.span9
label.checkbox
input(type="checkbox", ng-model="todo.done")
input(type="checkbox", ng-model="todo.done", ng-change="update(todo)")
del {{todo.description}}
div.span3.text-right
i {{todo.due | date:'MMM d, yyyy'}}

0 comments on commit e8fc93e

Please sign in to comment.