|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var App = angular.module('App', ['ngRoute']); |
| 4 | + |
| 5 | +App.factory('myHttpInterceptor', function($rootScope, $q) { |
| 6 | + return { |
| 7 | + 'requestError': function(config) { |
| 8 | + $rootScope.status = 'HTTP REQUEST ERROR ' + config; |
| 9 | + return config || $q.when(config); |
| 10 | + }, |
| 11 | + 'responseError': function(rejection) { |
| 12 | + $rootScope.status = 'HTTP RESPONSE ERROR ' + rejection.status + '\n' + |
| 13 | + rejection.data; |
| 14 | + return $q.reject(rejection); |
| 15 | + }, |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +App.factory('guestService', function($rootScope, $http, $q, $log) { |
| 20 | + $rootScope.status = 'Retrieving data...'; |
| 21 | + var deferred = $q.defer(); |
| 22 | + $http.get('rest/query') |
| 23 | + .success(function(data, status, headers, config) { |
| 24 | + $rootScope.guests = data; |
| 25 | + deferred.resolve(); |
| 26 | + $rootScope.status = ''; |
| 27 | + }); |
| 28 | + return deferred.promise; |
| 29 | +}); |
| 30 | + |
| 31 | +App.config(function($routeProvider) { |
| 32 | + $routeProvider.when('/', { |
| 33 | + controller : 'MainCtrl', |
| 34 | + templateUrl: '/partials/main.html', |
| 35 | + resolve : { 'guestService': 'guestService' }, |
| 36 | + }); |
| 37 | + $routeProvider.when('/invite', { |
| 38 | + controller : 'InsertCtrl', |
| 39 | + templateUrl: '/partials/insert.html', |
| 40 | + }); |
| 41 | + $routeProvider.when('/update/:id', { |
| 42 | + controller : 'UpdateCtrl', |
| 43 | + templateUrl: '/partials/update.html', |
| 44 | + resolve : { 'guestService': 'guestService' }, |
| 45 | + }); |
| 46 | + $routeProvider.otherwise({ |
| 47 | + redirectTo : '/' |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +App.config(function($httpProvider) { |
| 52 | + $httpProvider.interceptors.push('myHttpInterceptor'); |
| 53 | +}); |
| 54 | + |
| 55 | +App.controller('MainCtrl', function($scope, $rootScope, $log, $http, $routeParams, $location, $route) { |
| 56 | + |
| 57 | + $scope.invite = function() { |
| 58 | + $location.path('/invite'); |
| 59 | + }; |
| 60 | + |
| 61 | + $scope.update = function(guest) { |
| 62 | + $location.path('/update/' + guest.id); |
| 63 | + }; |
| 64 | + |
| 65 | + $scope.delete = function(guest) { |
| 66 | + $rootScope.status = 'Deleting guest ' + guest.id + '...'; |
| 67 | + $http.post('/rest/delete', {'id': guest.id}) |
| 68 | + .success(function(data, status, headers, config) { |
| 69 | + for (var i=0; i<$rootScope.guests.length; i++) { |
| 70 | + if ($rootScope.guests[i].id == guest.id) { |
| 71 | + $rootScope.guests.splice(i, 1); |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | + $rootScope.status = ''; |
| 76 | + }); |
| 77 | + }; |
| 78 | + |
| 79 | +}); |
| 80 | + |
| 81 | +App.controller('InsertCtrl', function($scope, $rootScope, $log, $http, $routeParams, $location, $route) { |
| 82 | + |
| 83 | + $scope.submitInsert = function() { |
| 84 | + var guest = { |
| 85 | + first : $scope.first, |
| 86 | + last : $scope.last, |
| 87 | + }; |
| 88 | + $rootScope.status = 'Creating...'; |
| 89 | + $http.post('/rest/insert', guest) |
| 90 | + .success(function(data, status, headers, config) { |
| 91 | + $rootScope.guests.push(data); |
| 92 | + $rootScope.status = ''; |
| 93 | + }); |
| 94 | + $location.path('/'); |
| 95 | + } |
| 96 | +}); |
| 97 | + |
| 98 | +App.controller('UpdateCtrl', function($routeParams, $rootScope, $scope, $log, $http, $location) { |
| 99 | + |
| 100 | + for (var i=0; i<$rootScope.guests.length; i++) { |
| 101 | + if ($rootScope.guests[i].id == $routeParams.id) { |
| 102 | + $scope.guest = angular.copy($rootScope.guests[i]); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + $scope.submitUpdate = function() { |
| 107 | + $rootScope.status = 'Updating...'; |
| 108 | + $http.post('/rest/update', $scope.guest) |
| 109 | + .success(function(data, status, headers, config) { |
| 110 | + for (var i=0; i<$rootScope.guests.length; i++) { |
| 111 | + if ($rootScope.guests[i].id == $scope.guest.id) { |
| 112 | + $rootScope.guests.splice(i,1); |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + $rootScope.guests.push(data); |
| 117 | + $rootScope.status = ''; |
| 118 | + }); |
| 119 | + $location.path('/'); |
| 120 | + }; |
| 121 | + |
| 122 | +}); |
| 123 | + |
0 commit comments