aboutsummaryrefslogtreecommitdiff
path: root/public/js
diff options
context:
space:
mode:
Diffstat (limited to 'public/js')
-rw-r--r--public/js/app.js1
-rw-r--r--public/js/controllers/todoController.js44
-rw-r--r--public/js/services/todoService.js16
3 files changed, 61 insertions, 0 deletions
diff --git a/public/js/app.js b/public/js/app.js
new file mode 100644
index 0000000..01ec9be
--- /dev/null
+++ b/public/js/app.js
@@ -0,0 +1 @@
+angular.module('toDoList', ['todoController', 'todoService']);
diff --git a/public/js/controllers/todoController.js b/public/js/controllers/todoController.js
new file mode 100644
index 0000000..eb73feb
--- /dev/null
+++ b/public/js/controllers/todoController.js
@@ -0,0 +1,44 @@
+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
diff --git a/public/js/services/todoService.js b/public/js/services/todoService.js
new file mode 100644
index 0000000..649c61f
--- /dev/null
+++ b/public/js/services/todoService.js
@@ -0,0 +1,16 @@
+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