Skip to content

Commit 43f111b

Browse files
author
Chris Sevilleja
committed
working on services
1 parent 89b9260 commit 43f111b

5 files changed

Lines changed: 67 additions & 55 deletions

File tree

public/core.js

Lines changed: 0 additions & 52 deletions
This file was deleted.

public/index.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020

2121
<!-- SPELLS -->
2222
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script><!-- load jquery -->
23-
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js"></script><!-- load angular -->
24-
<script src="core.js"></script>
23+
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js"></script><!-- load angular 1.2.4 -->
24+
25+
<script src="js/controllers/main.js"></script> <!-- load up our controller -->
26+
<script src="js/services/todos.js"></script> <!-- load our todo service -->
27+
<script src="js/core.js"></script> <!-- load our main application -->
2528

2629
</head>
27-
<!-- SET THE CONTROLLER AND GET ALL TODOS WITH INITIALIZE FUNCTION -->
30+
<!-- SET THE CONTROLLER -->
2831
<body ng-controller="mainController">
2932
<div class="container">
3033

public/js/controllers/main.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
angular.module('todoController', [])
2+
3+
// inject the Todo service factory into our controller
4+
.controller('mainController', function($scope, $http, Todos) {
5+
$scope.formData = {};
6+
7+
// GET =====================================================================
8+
// when landing on the page, get all todos and show them
9+
// use the service to get all the todos
10+
Todos.get()
11+
.success(function(data) {
12+
$scope.todos = data;
13+
});
14+
15+
// CREATE ==================================================================
16+
// when submitting the add form, send the text to the node API
17+
$scope.createTodo = function() {
18+
19+
// validate the formData to make sure that something is there
20+
// if form is empty, nothing will happen
21+
if (!$.isEmptyObject($scope.formData)) {
22+
23+
// call the create function from our service (returns a promise object)
24+
Todos.create($scope.formData)
25+
26+
// if successful creation, call our get function to get all the new todos
27+
.success(function(data) {
28+
$scope.formData = {}; // clear the form so our user is ready to enter another
29+
$scope.todos = data; // assign our new list of todos
30+
});
31+
}
32+
};
33+
34+
// DELETE ==================================================================
35+
// delete a todo after checking it
36+
$scope.deleteTodo = function(id) {
37+
Todos.delete(id)
38+
// if successful creation, call our get function to get all the new todos
39+
.success(function(data) {
40+
$scope.todos = data; // assign our new list of todos
41+
});
42+
};
43+
});

public/js/core.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
angular.module('scotchTodo', ['todoController', 'todoService']);

public/js/services/todos.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
angular.module('todoService', [])
2+
3+
// super simple service
4+
// each function returns a promise object
5+
.factory('Todos', function($http) {
6+
return {
7+
get : function() {
8+
return $http.get('/api/todos');
9+
},
10+
create : function(todoData) {
11+
return $http.post('/api/todos', todoData);
12+
},
13+
delete : function(id) {
14+
return $http.delete('/api/todos/' + id);
15+
}
16+
}
17+
});

0 commit comments

Comments
 (0)