Skip to content

Setting up an express application #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
13 changes: 13 additions & 0 deletions app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

var angular = require('angular');

angular.module('todoListApp', []);

require('./scripts/controllers/main.js');

require('./scripts/controllers/todo.js');

require('./scripts/services/data.js');

require('./scripts/directives/todo.js');
27 changes: 27 additions & 0 deletions app/scripts/controllers/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

var angular = require('angular');

angular.module('todoListApp')
.controller('mainCtrl', function($scope, $log, $interval, dataService){

$scope.seconds = 0;

$scope.counter = function(){
$scope.seconds++;
$log.error($scope.seconds + ' have passed');
};

$interval($scope.counter, 1000, 10);

dataService.getTodos(function(response){
var todos = response.data;
$scope.todos = todos;
});

$scope.addTodo = function() {
$scope.todos.unshift({name: 'This is a new todo.',
completed: false});
};

});
25 changes: 25 additions & 0 deletions app/scripts/controllers/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

angular.module('todoListApp')
.controller('todoCtrl', function($scope, dataService) {
$scope.deleteTodo = function(todo, index) {
$scope.todos.splice(index, 1);
dataService.deleteTodo(todo);
};

$scope.saveTodos = function() {
var filteredTodos = $scope.todos.filter(function(todo){
if(todo.edited) {
return todo;
}
});
dataService.saveTodos(filteredTodos)
.finally($scope.resetTodoState());
};

$scope.resetTodoState = function(){
$scope.todos.forEach(function(todo){
todo.edited = false;
});
};
});
10 changes: 10 additions & 0 deletions app/scripts/directives/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

angular.module('todoListApp')
.directive('todo', function(){
return {
templateUrl: 'templates/todo.html',
replace: true,
controller: 'todoCtrl'
}
});
34 changes: 34 additions & 0 deletions app/scripts/services/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

angular.module('todoListApp')
.service('dataService', function($http, $q) {
this.getTodos = function(cb) {
$http.get('/api/todos').then(cb);
};

this.deleteTodo = function(todo) {
console.log('I deleted the ' + todo.name + ' todo!');
};

this.saveTodos = function(todos) {
console.log(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');
});
};

});
16 changes: 16 additions & 0 deletions mock/todos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"todosList": [
{
"name": "build a robot"
},
{
"name": "fight dragons"
},
{
"name": "learn how to sing"
},
{
"name": "practice the piano"
}
]
}
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "mean-todo",
"version": "1.0.0",
"description": "This is the course repo for [Building a MEAN Application](https://teamtreehouse.com/library/building-a-mean-application) on [Treehouse](https://teamtreehouse.com/) by Huston Hedinger and Ken Howard.",
"main": "src/app.js",
"dependencies": {
"angular": "^1.5.8",
"body-parser": "1.15.2",
"express": "4.14.0",
"mongoose": "4.6.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/michellesri/mean-todo.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/michellesri/mean-todo/issues"
},
"homepage": "https://github.com/michellesri/mean-todo#readme"
}
9 changes: 9 additions & 0 deletions public/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# angular-basics
The Angular Basics course is taught in workspaces. This is the code for the final course app.

# set-up
```
git clone <this-repo>
cd <this-repo>
http-server -p 3000
# visit localhost:3000 to see the todoListApp
25 changes: 25 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!doctype html>
<html lang="en">
<head>
<title></title>
<link href='https://fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="/styles/main.css" type="text/css">
</head>
<body ng-app="todoListApp">
<ng-include src="'templates/navbar.html'"></ng-include>

<h1>My TODOs!</h1>

<div class="list" ng-controller="mainCtrl">
<div class="add">
<a href="#" ng-click="addTodo()">
+ Add a New Task</a>
</div>
<todo></todo>
</div>


<script src="/scripts/vendor.bundle.js"></script>
<script src="/scripts/todo.bundle.js"></script>
</body>
</html>
9 changes: 9 additions & 0 deletions public/mock/todos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{"name": "clean the house"},
{"name": "water the dog"},
{"name": "feed the lawn"},
{"name": "pay dem bills"},
{"name": "run"},
{"name": "swim"}
]

16 changes: 16 additions & 0 deletions public/scripts/controllers/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

angular.module('todoListApp')
.controller('mainCtrl', function($scope, dataService){

dataService.getTodos(function(response){
var todos = response.data.todos.todosList;
$scope.todos = todos;
});

$scope.addTodo = function() {
$scope.todos.unshift({name: 'This is a new todo.',
completed: false});
};

});
17 changes: 17 additions & 0 deletions public/scripts/services/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

angular.module('todoListApp')
.service('dataService', function($http) {
this.getTodos = function(cb) {
$http.get('/api/todos').then(cb);
};

this.deleteTodo = function(todo) {
console.log('I deleted the ' + todo.name + ' todo!');
};

this.saveTodos = function(todos) {
console.log('I saved ' + todos.length + ' todos!');
};

});
142 changes: 142 additions & 0 deletions public/scripts/todo.bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
webpackJsonp([0],[
/* 0 */
/***/ function(module, exports, __webpack_require__) {

'use strict';

var angular = __webpack_require__(1);

angular.module('todoListApp', []);

__webpack_require__(3);

__webpack_require__(4);

__webpack_require__(5);

__webpack_require__(6);


/***/ },
/* 1 */,
/* 2 */,
/* 3 */
/***/ function(module, exports, __webpack_require__) {

'use strict';

var angular = __webpack_require__(1);

angular.module('todoListApp')
.controller('mainCtrl', function($scope, $log, $interval, dataService){

$scope.seconds = 0;

$scope.counter = function(){
$scope.seconds++;
$log.error($scope.seconds + ' have passed');
};

$interval($scope.counter, 1000, 10);

dataService.getTodos(function(response){
var todos = response.data;
$scope.todos = todos;
});

$scope.addTodo = function() {
$scope.todos.unshift({name: 'This is a new todo.',
completed: false});
};

});


/***/ },
/* 4 */
/***/ function(module, exports) {

'use strict';

angular.module('todoListApp')
.controller('todoCtrl', function($scope, dataService) {
$scope.deleteTodo = function(todo, index) {
$scope.todos.splice(index, 1);
dataService.deleteTodo(todo);
};

$scope.saveTodos = function() {
var filteredTodos = $scope.todos.filter(function(todo){
if(todo.edited) {
return todo;
}
});
dataService.saveTodos(filteredTodos)
.finally($scope.resetTodoState());
};

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


/***/ },
/* 5 */
/***/ function(module, exports) {

'use strict';

angular.module('todoListApp')
.service('dataService', function($http, $q) {
this.getTodos = function(cb) {
$http.get('/api/todos').then(cb);
};

this.deleteTodo = function(todo) {
console.log('I deleted the ' + todo.name + ' todo!');
};

this.saveTodos = function(todos) {
console.log(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');
});
};

});


/***/ },
/* 6 */
/***/ function(module, exports) {

'use strict';

angular.module('todoListApp')
.directive('todo', function(){
return {
templateUrl: 'templates/todo.html',
replace: true,
controller: 'todoCtrl'
}
});

/***/ }
]);
Loading