Skip to content

Commit 69eafa7

Browse files
author
Chris Sevilleja
committed
delete working
1 parent b9bb5e3 commit 69eafa7

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

public/js/core.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@ var scotchTodo = angular.module('scotchTodo', []);
33
function mainController($scope, $http) {
44
$scope.formData = {};
55

6+
// when landing on the page, get all todos and show them
67
$scope.initialize = function() {
78
$http.get('/api/todos')
89
.success(function(data) {
910
$scope.todos = data;
11+
console.log(data);
1012
})
1113
.error(function(data) {
1214
console.log('Error: ' + data);
1315
});
1416
};
1517

18+
// when submitting the add form, send the text to the node API
1619
$scope.createTodo = function() {
1720
$http.post('/api/todos', $scope.formData)
1821
.success(function(data) {
@@ -24,12 +27,16 @@ function mainController($scope, $http) {
2427
});
2528
};
2629

27-
$scope.changeTodo = function() {
28-
29-
};
30-
31-
$scope.deleteTodo = function() {
32-
30+
// delete a todo after checking it
31+
$scope.deleteTodo = function(id) {
32+
$http.delete('/api/todos/' + id)
33+
.success(function(data) {
34+
$scope.todos = data;
35+
console.log(data);
36+
})
37+
.error(function(data) {
38+
console.log('Error: ' + data);
39+
});
3340
};
3441

3542
}

public/views/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ <h1>I'm a Todo-aholic</h1>
4141

4242
<div class="checkbox" ng-repeat="todo in todos">
4343
<label>
44-
<input type="checkbox"> {{ todo.text }}
44+
<input type="checkbox" ng-click="deleteTodo(todo._id)"> {{ todo.text }}
4545
</label>
4646
</div>
4747

server.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var Todo = mongoose.model('Todo', {
3838
});
3939
});
4040

41-
// create todo
41+
// create todo and send back all todos after creation
4242
app.post('/api/todos', function(req, res) {
4343

4444
// create a todo, information comes from AJAX request from Angular
@@ -49,22 +49,31 @@ var Todo = mongoose.model('Todo', {
4949
if (err)
5050
res.send(err);
5151

52-
// use mongoose to get all todos in the database
52+
// get and return all the todos after you create another
5353
Todo.find(function(err, todos) {
54-
55-
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
5654
if (err)
5755
res.send(err)
58-
59-
res.json(todos); // return all todos in JSON format
56+
res.json(todos);
6057
});
6158
});
6259

6360
});
6461

6562
// delete a todo
6663
app.delete('/api/todos/:todo_id', function(req, res) {
67-
res.send('delete todo #' + req.params.todo_id + ' (via api)');
64+
Todo.remove({
65+
_id : req.params.todo_id
66+
}, function(err, todo) {
67+
if (err)
68+
res.send(err);
69+
70+
// get and return all the todos after you create another
71+
Todo.find(function(err, todos) {
72+
if (err)
73+
res.send(err)
74+
res.json(todos);
75+
});
76+
});
6877
});
6978

7079
// application -------------------------------------------------------------

0 commit comments

Comments
 (0)