diff options
| -rw-r--r-- | app/routes.js | 11 | ||||
| -rw-r--r-- | public/js/controllers/todoController.js | 81 | ||||
| -rw-r--r-- | public/js/services/todoService.js | 28 | ||||
| -rw-r--r-- | server.js | 18 |
4 files changed, 63 insertions, 75 deletions
diff --git a/app/routes.js b/app/routes.js index 7b596dc..724af11 100644 --- a/app/routes.js +++ b/app/routes.js @@ -1,18 +1,15 @@ // get the todos collection var todoCollect = require('./models/todo'); - // use mongo to get all the items in db function getTodos (res) { todoCollect.find(function (err, todos) { if (err) res.send(err); - - res.json(todos); // return all todos in JSON format - // console.log(todos); + // return all todos in JSON format + res.json(todos); }); } - // ===========================API=============================== module.exports = function (app) { @@ -30,7 +27,6 @@ module.exports = function (app) { done: false }, function (err, todo) { if (err) res.send(err); - // 'refresh' to-do list getTodos(res); }); @@ -43,7 +39,6 @@ module.exports = function (app) { _id: req.params.todo_id }, function (err, todo) { if (err) res.send(err); - // 'refresh' to-do list getTodos(res); }); @@ -53,4 +48,4 @@ module.exports = function (app) { app.get('*', function (req, res) { res.sendFile(__dirname + '/public/index.html'); }); -};
\ No newline at end of file +}; diff --git a/public/js/controllers/todoController.js b/public/js/controllers/todoController.js index eb73feb..08e5a3f 100644 --- a/public/js/controllers/todoController.js +++ b/public/js/controllers/todoController.js @@ -1,44 +1,39 @@ angular.module('todoController', []) - - // inject service factory into our controller - .controller('mainController', ['$scope','$http','Todos', function($scope, $http, Todos) { - $scope.formData = {}; - $scope.loading = true; - - // on first visit 'refresh' the todo list - Todos.get() - .success(function(data) { - $scope.todos = data; - $scope.loading = false; - }); - - // when submitting the add form, send formdata to api - $scope.createTodo = function() { - - // validate the formData to make sure that something is there - if ($scope.formData.text != undefined) { - $scope.loading = true; - - Todos.create($scope.formData) - - // if successful creation, 'refresh' todo list - .success(function(data) { - $scope.loading = false; - $scope.formData = ""; // clear the form - $scope.todos = data; // assign our new list of todo - }); - } - }; - - // delete a todo when pressed checkbox - $scope.deleteTodo = function(id) { - $scope.loading = true; - - Todos.delete(id) - // if successful, 'refresh' the list with latest data - .success(function(data) { - $scope.loading = false; - $scope.todos = data; - }); - }; - }]);
\ No newline at end of file + .controller('mainController', ['$scope', '$http', 'Todos', function($scope, $http, Todos) { + $scope.formData = {}; + $scope.loading = true; + + // on first visit 'refresh' the todo list + Todos.get() + .success(function(data) { + $scope.todos = data; + $scope.loading = false; + }); + + // when submitting the add form, send formdata to api + $scope.createTodo = function() { + + // validate the formData to make sure that something is there + if ($scope.formData.text != undefined) { + $scope.loading = true; + + Todos.create($scope.formData) + .success(function(data) { // refresh + $scope.loading = false; + $scope.formData = ""; // clear the form + $scope.todos = data; // assign our new list of todo + }); + } + }; + + // delete a todo when pressed checkbox + $scope.deleteTodo = function(id) { + $scope.loading = true; + + Todos.delete(id) + .success(function(data) { // refresh + $scope.loading = false; + $scope.todos = data; + }); + }; + }]); diff --git a/public/js/services/todoService.js b/public/js/services/todoService.js index 649c61f..a51db95 100644 --- a/public/js/services/todoService.js +++ b/public/js/services/todoService.js @@ -1,16 +1,14 @@ angular.module('todoService', []) - - // use the api - .factory('Todos', ['$http', function($http) { - return { - get : function() { - return $http.get('/api/todos'); - }, - create : function(data) { - return $http.post('/api/todos', data); - }, - delete : function(id) { - return $http.delete('/api/todos/' + id); - } - } - }]);
\ No newline at end of file + .factory('Todos', ['$http', function($http) { + return { + get : function() { + return $http.get('/api/todos'); + }, + create : function(data) { + return $http.post('/api/todos', data); + }, + delete : function(id) { + return $http.delete('/api/todos/' + id); + } + } + }]); @@ -1,25 +1,26 @@ // set up the app var express = require('express'); var app = express(); -var mongoose = require('mongoose'); // for mongodb -var port = process.env.PORT || 3000; // the port -// var database = require('./config/database'); // load the database configuration (uri) +var mongoose = require('mongoose'); // for mongodb +var port = process.env.PORT || 3000; // the port +// var database = require('./config/database'); // load the database configuration (uri) // var morgan = require('morgan'); // for debugging var bodyParser = require('body-parser'); // for ease of use var methodOverride = require('method-override'); // just in case // set up for functionality -mongoose.connect('mongodb://test:admin@ds139665.mlab.com:39665/to_list'); // connect to mongodb -// mongoose.connect('mongodb://localhost/2701'); // connect to mongodb +mongoose.connect('mongodb://test:admin@ds139665.mlab.com:39665/to_list'); // connect to mongodb +// mongoose.connect('mongodb://localhost/2701'); // connect to mongodb -app.use(express.static('./public')); // set the static files location /public/img will be /img for users +// set the static files location /public/img will be /img for users +app.use(express.static('./public')); // app.use(morgan('dev')); // log every request to the console app.use(bodyParser.urlencoded({'extended': 'true'})); // parse application ...url app.use(bodyParser.json()); // parse application/json app.use(bodyParser.json({type: 'application/vnd.api+json'})); // parse application/vnd.api+json as json -app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request - +// override with the X-HTTP-Method-Override header in the request +app.use(methodOverride('X-HTTP-Method-Override')); // connect with api require('./app/routes.js')(app); @@ -27,4 +28,3 @@ require('./app/routes.js')(app); app.listen(port); console.log("Listening on port " + port); - |
