diff --git a/app/routes.js b/app/routes.js index 17a201567..20d05cd51 100644 --- a/app/routes.js +++ b/app/routes.js @@ -1,57 +1,76 @@ var Todo = require('./models/todo'); function getTodos(res){ - Todo.find(function(err, todos) { + Todo.find(function(err, todos) { - // if there is an error retrieving, send the error. nothing after res.send(err) will execute - if (err) - res.send(err) + // if there is an error retrieving, send the error. nothing after res.send(err) will execute + if (err) + res.send(err) - res.json(todos); // return all todos in JSON format - }); + res.json(todos); // return all todos in JSON format + }); }; module.exports = function(app) { - // api --------------------------------------------------------------------- - // get all todos - app.get('/api/todos', function(req, res) { - - // use mongoose to get all todos in the database - getTodos(res); - }); - - // create todo and send back all todos after creation - app.post('/api/todos', function(req, res) { - - // create a todo, information comes from AJAX request from Angular - Todo.create({ - text : req.body.text, - done : false - }, function(err, todo) { - if (err) - res.send(err); - - // get and return all the todos after you create another - getTodos(res); - }); - - }); - - // delete a todo - app.delete('/api/todos/:todo_id', function(req, res) { - Todo.remove({ - _id : req.params.todo_id - }, function(err, todo) { - if (err) - res.send(err); - - getTodos(res); - }); - }); - - // application ------------------------------------------------------------- - app.get('*', function(req, res) { - res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end) - }); + // api --------------------------------------------------------------------- + // get all todos + app.get('/api/todos', function(req, res) { + + // use mongoose to get all todos in the database + getTodos(res); + }); + + // create todo and send back all todos after creation + app.post('/api/todos', function(req, res) { + + // create a todo, information comes from AJAX request from Angular + Todo.create({ + text : req.body.text, + done : false + }, function(err, todo) { + if (err) + res.send(err); + + // get and return all the todos after you create another + getTodos(res); + }); + + }); + + app.put('/api/todos', function(req, res){ + console.log(req.body); + var todoId = req.body.todoId + , todoText = req.body.todoText; + + Todo.update({ + _id:todoId + }, + { + $set:{text:todoText} + }, + function(err, todo){ + if(err) + res.send(err); + res.send("Success"); + } + ); + }); + + // delete a todo + app.delete('/api/todos/:todo_id', function(req, res) { + Todo.remove({ + _id : req.params.todo_id + }, function(err, todo) { + if (err) + res.send(err); + + getTodos(res); + }); + }); + + // application ------------------------------------------------------------- + app.get('*', function(req, res) { + res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end) + }); }; \ No newline at end of file diff --git a/public/index.html b/public/index.html index 3bb866403..a4c086ad4 100644 --- a/public/index.html +++ b/public/index.html @@ -3,81 +3,87 @@ - - - + + + - Node/Angular Todo App + Node/Angular Todo App - - - - + + + + - - + + - - - + + + -
- - -
-

I'm a Todo-aholic {{ todos.length }}

-
- - -
-
- - - - -
- -
- -

- -

- -
-
- - -
-
-
-
- - - -
- - - -
-
-
- -
-

A demo by Scotch.

-

Read the tutorial.

-
- -
+
+ + +
+

I'm a Todo-aholic {{ todos.length }}

+
+ + +
+
+ + + + +
+ + + + + +
+ +

+ +

+ +
+
+ + +
+
+
+
+ + + +
+ + + +
+
+
+ +
+

A demo by Scotch.

+

Read the tutorial.

+
+ +
diff --git a/public/js/controllers/main.js b/public/js/controllers/main.js index 7cb2ce277..74f1abeaf 100644 --- a/public/js/controllers/main.js +++ b/public/js/controllers/main.js @@ -1,50 +1,71 @@ angular.module('todoController', []) - // inject the Todo service factory into our controller - .controller('mainController', ['$scope','$http','Todos', function($scope, $http, Todos) { - $scope.formData = {}; - $scope.loading = true; - - // GET ===================================================================== - // when landing on the page, get all todos and show them - // use the service to get all the todos - Todos.get() - .success(function(data) { - $scope.todos = data; - $scope.loading = false; - }); - - // CREATE ================================================================== - // when submitting the add form, send the text to the node API - $scope.createTodo = function() { - - // validate the formData to make sure that something is there - // if form is empty, nothing will happen - if ($scope.formData.text != undefined) { - $scope.loading = true; - - // call the create function from our service (returns a promise object) - Todos.create($scope.formData) - - // if successful creation, call our get function to get all the new todos - .success(function(data) { - $scope.loading = false; - $scope.formData = {}; // clear the form so our user is ready to enter another - $scope.todos = data; // assign our new list of todos - }); - } - }; - - // DELETE ================================================================== - // delete a todo after checking it - $scope.deleteTodo = function(id) { - $scope.loading = true; - - Todos.delete(id) - // if successful creation, call our get function to get all the new todos - .success(function(data) { - $scope.loading = false; - $scope.todos = data; // assign our new list of todos - }); - }; - }]); \ No newline at end of file + // inject the Todo service factory into our controller + .controller('mainController', ['$scope','$http','Todos', function($scope, $http, Todos) { + $scope.formData = {}; + $scope.loading = true; + $scope.editable=[]; + + // GET ===================================================================== + // when landing on the page, get all todos and show them + // use the service to get all the todos + Todos.get() + .success(function(data) { + $scope.todos = data; + for(var iter=0; iter