From 89b926044381e5576a29f5480dd855542e0ff328 Mon Sep 17 00:00:00 2001 From: Chris Sevilleja Date: Sat, 7 Dec 2013 21:54:16 -0800 Subject: [PATCH 01/18] working on services --- public/core.js | 20 +++++++++++++++++--- public/index.html | 2 +- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/public/core.js b/public/core.js index 8b8889dc0..a19c83d2d 100644 --- a/public/core.js +++ b/public/core.js @@ -1,10 +1,24 @@ var scotchTodo = angular.module('scotchTodo', []); -function mainController($scope, $http) { +scotchTodo.factory('todoFactory', function($http) { + return { + get : function() { + return $http.get('/api/todos'); + }, + create : function() { + $http.post('/api/todos', $scope.formData); + }, + delete : function() { + + } + } +}); + +function mainController($scope, $http, todoFactory) { $scope.formData = {}; // when landing on the page, get all todos and show them - $http.get('/api/todos') + todoFactory.get() .success(function(data) { $scope.todos = data; }) @@ -14,7 +28,7 @@ function mainController($scope, $http) { // when submitting the add form, send the text to the node API $scope.createTodo = function() { - $http.post('/api/todos', $scope.formData) + .success(function(data) { $('input').val(''); $scope.todos = data; diff --git a/public/index.html b/public/index.html index b947c3225..5514859ea 100644 --- a/public/index.html +++ b/public/index.html @@ -20,7 +20,7 @@ - + From 43f111b1193258522d57b3dca35d5474875cf084 Mon Sep 17 00:00:00 2001 From: Chris Sevilleja Date: Sun, 8 Dec 2013 21:50:56 -0800 Subject: [PATCH 02/18] working on services --- public/core.js | 52 ----------------------------------- public/index.html | 9 ++++-- public/js/controllers/main.js | 43 +++++++++++++++++++++++++++++ public/js/core.js | 1 + public/js/services/todos.js | 17 ++++++++++++ 5 files changed, 67 insertions(+), 55 deletions(-) delete mode 100644 public/core.js create mode 100644 public/js/controllers/main.js create mode 100644 public/js/core.js create mode 100644 public/js/services/todos.js diff --git a/public/core.js b/public/core.js deleted file mode 100644 index a19c83d2d..000000000 --- a/public/core.js +++ /dev/null @@ -1,52 +0,0 @@ -var scotchTodo = angular.module('scotchTodo', []); - -scotchTodo.factory('todoFactory', function($http) { - return { - get : function() { - return $http.get('/api/todos'); - }, - create : function() { - $http.post('/api/todos', $scope.formData); - }, - delete : function() { - - } - } -}); - -function mainController($scope, $http, todoFactory) { - $scope.formData = {}; - - // when landing on the page, get all todos and show them - todoFactory.get() - .success(function(data) { - $scope.todos = data; - }) - .error(function(data) { - console.log('Error: ' + data); - }); - - // when submitting the add form, send the text to the node API - $scope.createTodo = function() { - - .success(function(data) { - $('input').val(''); - $scope.todos = data; - }) - .error(function(data) { - console.log('Error: ' + data); - }); - }; - - // delete a todo after checking it - $scope.deleteTodo = function(id) { - $http.delete('/api/todos/' + id) - .success(function(data) { - $scope.todos = data; - }) - .error(function(data) { - console.log('Error: ' + data); - }); - }; - -} \ No newline at end of file diff --git a/public/index.html b/public/index.html index 5514859ea..2d7d6d032 100644 --- a/public/index.html +++ b/public/index.html @@ -20,11 +20,14 @@ - - + + + + + - +
diff --git a/public/js/controllers/main.js b/public/js/controllers/main.js new file mode 100644 index 000000000..ccdae788b --- /dev/null +++ b/public/js/controllers/main.js @@ -0,0 +1,43 @@ +angular.module('todoController', []) + + // inject the Todo service factory into our controller + .controller('mainController', function($scope, $http, Todos) { + $scope.formData = {}; + + // GET ===================================================================== + // when landing on the page, get all todos and show them + // use the service to get all the todos + Todos.get() + .success(function(data) { + $scope.todos = data; + }); + + // CREATE ================================================================== + // when submitting the add form, send the text to the node API + $scope.createTodo = function() { + + // validate the formData to make sure that something is there + // if form is empty, nothing will happen + if (!$.isEmptyObject($scope.formData)) { + + // call the create function from our service (returns a promise object) + Todos.create($scope.formData) + + // if successful creation, call our get function to get all the new todos + .success(function(data) { + $scope.formData = {}; // clear the form so our user is ready to enter another + $scope.todos = data; // assign our new list of todos + }); + } + }; + + // DELETE ================================================================== + // delete a todo after checking it + $scope.deleteTodo = function(id) { + Todos.delete(id) + // if successful creation, call our get function to get all the new todos + .success(function(data) { + $scope.todos = data; // assign our new list of todos + }); + }; + }); \ No newline at end of file diff --git a/public/js/core.js b/public/js/core.js new file mode 100644 index 000000000..28dbeed2f --- /dev/null +++ b/public/js/core.js @@ -0,0 +1 @@ +angular.module('scotchTodo', ['todoController', 'todoService']); diff --git a/public/js/services/todos.js b/public/js/services/todos.js new file mode 100644 index 000000000..ddf2784c9 --- /dev/null +++ b/public/js/services/todos.js @@ -0,0 +1,17 @@ +angular.module('todoService', []) + + // super simple service + // each function returns a promise object + .factory('Todos', function($http) { + return { + get : function() { + return $http.get('/api/todos'); + }, + create : function(todoData) { + return $http.post('/api/todos', todoData); + }, + delete : function(id) { + return $http.delete('/api/todos/' + id); + } + } + }); \ No newline at end of file From 621d73c3997564be635dbb9dbce00603411508b5 Mon Sep 17 00:00:00 2001 From: Chris Sevilleja Date: Thu, 17 Apr 2014 10:32:55 -0700 Subject: [PATCH 03/18] Update index.html --- public/index.html | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/public/index.html b/public/index.html index 2d7d6d032..78b1ba920 100644 --- a/public/index.html +++ b/public/index.html @@ -19,7 +19,6 @@ - @@ -74,4 +73,4 @@

I'm a Todo-aholic {{ todos.length }} - \ No newline at end of file + From 049788cb9a6a3cc58289be7b4fb949dc16a1c194 Mon Sep 17 00:00:00 2001 From: Chris Sevilleja Date: Thu, 17 Apr 2014 10:39:17 -0700 Subject: [PATCH 04/18] fixing things --- public/js/controllers/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/js/controllers/main.js b/public/js/controllers/main.js index ccdae788b..13e4f9250 100644 --- a/public/js/controllers/main.js +++ b/public/js/controllers/main.js @@ -18,7 +18,7 @@ angular.module('todoController', []) // validate the formData to make sure that something is there // if form is empty, nothing will happen - if (!$.isEmptyObject($scope.formData)) { + if ($scope.formData.text != undefined) { // call the create function from our service (returns a promise object) Todos.create($scope.formData) From c53b479cbc9cc6d705ed5d4c8c9809f23420135d Mon Sep 17 00:00:00 2001 From: Chris Sevilleja Date: Fri, 18 Apr 2014 07:42:46 -0700 Subject: [PATCH 05/18] adding loading --- public/index.html | 7 +++++++ public/js/controllers/main.js | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/public/index.html b/public/index.html index 9148fc5b9..3bb866403 100644 --- a/public/index.html +++ b/public/index.html @@ -11,6 +11,7 @@ +