aboutsummaryrefslogtreecommitdiff
path: root/public/js/controllers/todoController.js
blob: 08e5a3f71d95b7811f6eaea82b8457a9b01f21e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
angular.module('todoController', [])
  .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)
                  .success(function(data) {         // refresh
                      $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)
              .success(function(data) {             // refresh
                  $scope.loading = false;
                  $scope.todos = data;
          });
      };
  }]);