Skip to content

Commit

Permalink
Merge pull request scotch-io#3 from Xuefeng-Zhu/master
Browse files Browse the repository at this point in the history
Replace repeated code with one function and bug fix
  • Loading branch information
Chris Sevilleja committed Nov 17, 2014
2 parents 90b7698 + a292d8f commit 5f31fd7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
33 changes: 14 additions & 19 deletions app/routes.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
var Todo = require('./models/todo');

module.exports = function(app) {

// api ---------------------------------------------------------------------
// get all todos
app.get('/api/todos', function(req, res) {

// use mongoose to get all todos in the database
Todo.find(function(err, todos) {
function getTodos(res){
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)

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
Expand All @@ -29,11 +33,7 @@ module.exports = function(app) {
res.send(err);

// get and return all the todos after you create another
Todo.find(function(err, todos) {
if (err)
res.send(err)
res.json(todos);
});
getTodos(res);
});

});
Expand All @@ -46,12 +46,7 @@ module.exports = function(app) {
if (err)
res.send(err);

// get and return all the todos after you create another
Todo.find(function(err, todos) {
if (err)
res.send(err)
res.json(todos);
});
getTodos(res);
});
});

Expand Down
2 changes: 1 addition & 1 deletion public/js/controllers/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ angular.module('todoController', [])
// CREATE ==================================================================
// when submitting the add form, send the text to the node API
$scope.createTodo = function() {
$scope.loading = true;

// 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)
Expand Down

0 comments on commit 5f31fd7

Please sign in to comment.