Skip to content

Commit

Permalink
Updated the Angular app to create and update data
Browse files Browse the repository at this point in the history
  • Loading branch information
smashdevcode committed Mar 2, 2016
1 parent 94b07a2 commit dbdefaa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
8 changes: 7 additions & 1 deletion app/scripts/controllers/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ function TodoCtrl ($scope, dataService) {
return todo
};
})
dataService.saveTodos(filteredTodos);
dataService.saveTodos(filteredTodos)
.finally($scope.resetTodoState());
};

$scope.resetTodoState = function() {
$scope.todos.forEach(function(todo) {
todo.edited = false;
});
}
}

module.exports = TodoCtrl;
19 changes: 17 additions & 2 deletions app/scripts/services/data.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

function DataService ($http) {
function DataService ($http, $q) {

this.getTodos = function(cb) {
$http.get('/api/todos').then(cb);
Expand All @@ -11,7 +11,22 @@ function DataService ($http) {
};

this.saveTodos = function(todos) {
console.log("I saved " + todos.length + " todos!");
var queue = [];
todos.forEach(function(todo) {
var request;
if(!todo._id) {
request = $http.post('/api/todos', todo);
} else {
request = $http.put('/api/todos/' + todo._id, todo).then(function(result) {
todo = result.data.todo;
return todo;
});
}
queue.push(request);
});
return $q.all(queue).then(function(results) {
console.log("I saved " + todos.length + " todos!");
});
};

}
Expand Down

0 comments on commit dbdefaa

Please sign in to comment.