forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
123 lines (106 loc) · 3.29 KB
/
Copy pathapp.js
File metadata and controls
123 lines (106 loc) · 3.29 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict';
var App = angular.module('App', ['ngRoute']);
App.factory('myHttpInterceptor', function($rootScope, $q) {
return {
'requestError': function(config) {
$rootScope.status = 'HTTP REQUEST ERROR ' + config;
return config || $q.when(config);
},
'responseError': function(rejection) {
$rootScope.status = 'HTTP RESPONSE ERROR ' + rejection.status + '\n' +
rejection.data;
return $q.reject(rejection);
},
};
});
App.factory('guestService', function($rootScope, $http, $q, $log) {
$rootScope.status = 'Retrieving data...';
var deferred = $q.defer();
$http.get('rest/query')
.success(function(data, status, headers, config) {
$rootScope.guests = data;
deferred.resolve();
$rootScope.status = '';
});
return deferred.promise;
});
App.config(function($routeProvider) {
$routeProvider.when('/', {
controller : 'MainCtrl',
templateUrl: '/partials/main.html',
resolve : { 'guestService': 'guestService' },
});
$routeProvider.when('/invite', {
controller : 'InsertCtrl',
templateUrl: '/partials/insert.html',
});
$routeProvider.when('/update/:id', {
controller : 'UpdateCtrl',
templateUrl: '/partials/update.html',
resolve : { 'guestService': 'guestService' },
});
$routeProvider.otherwise({
redirectTo : '/'
});
});
App.config(function($httpProvider) {
$httpProvider.interceptors.push('myHttpInterceptor');
});
App.controller('MainCtrl', function($scope, $rootScope, $log, $http, $routeParams, $location, $route) {
$scope.invite = function() {
$location.path('/invite');
};
$scope.update = function(guest) {
$location.path('/update/' + guest.id);
};
$scope.delete = function(guest) {
$rootScope.status = 'Deleting guest ' + guest.id + '...';
$http.post('/rest/delete', {'id': guest.id})
.success(function(data, status, headers, config) {
for (var i=0; i<$rootScope.guests.length; i++) {
if ($rootScope.guests[i].id == guest.id) {
$rootScope.guests.splice(i, 1);
break;
}
}
$rootScope.status = '';
});
};
});
App.controller('InsertCtrl', function($scope, $rootScope, $log, $http, $routeParams, $location, $route) {
$scope.submitInsert = function() {
var guest = {
first : $scope.first,
last : $scope.last,
};
$rootScope.status = 'Creating...';
$http.post('/rest/insert', guest)
.success(function(data, status, headers, config) {
$rootScope.guests.push(data);
$rootScope.status = '';
});
$location.path('/');
}
});
App.controller('UpdateCtrl', function($routeParams, $rootScope, $scope, $log, $http, $location) {
for (var i=0; i<$rootScope.guests.length; i++) {
if ($rootScope.guests[i].id == $routeParams.id) {
$scope.guest = angular.copy($rootScope.guests[i]);
}
}
$scope.submitUpdate = function() {
$rootScope.status = 'Updating...';
$http.post('/rest/update', $scope.guest)
.success(function(data, status, headers, config) {
for (var i=0; i<$rootScope.guests.length; i++) {
if ($rootScope.guests[i].id == $scope.guest.id) {
$rootScope.guests.splice(i,1);
break;
}
}
$rootScope.guests.push(data);
$rootScope.status = '';
});
$location.path('/');
};
});