commonroutesServer/app/www/js/controllers.js
app/www/js/controllers.jsRaw
1angular.module('starter.controllers', [])
2
3.controller('AppCtrl', function($scope, $ionicModal, $timeout) {
4
5 // With the new view caching in Ionic, Controllers are only called
6 // when they are recreated or on app start, instead of every page change.
7 // To listen for when this page is active (for example, to refresh data),
8 // listen for the $ionicView.enter event:
9 //$scope.$on('$ionicView.enter', function(e) {
10 //});
11
12 // Form data for the login modal
13 $scope.loginData = {};
14
15 // Create the login modal that we will use later
16 $ionicModal.fromTemplateUrl('templates/login.html', {
17 scope: $scope
18 }).then(function(modal) {
19 $scope.modal = modal;
20 });
21
22 // Triggered in the login modal to close it
23 $scope.closeLogin = function() {
24 $scope.modal.hide();
25 };
26
27 // Open the login modal
28 $scope.login = function() {
29 $scope.modal.show();
30 };
31
32 // Perform the login action when the user submits the login form
33 $scope.doLogin = function() {
34 console.log('Doing login', $scope.loginData);
35
36 // Simulate a login delay. Remove this and replace with your login
37 // code if using a login system
38 $timeout(function() {
39 $scope.closeLogin();
40 }, 1000);
41 };
42})
43
44
45.controller('TravelsCtrl', function($scope, $http, $ionicModal, $timeout) {
46 $scope.travels="";
47
48 $scope.travels=JSON.parse(localStorage.getItem('c_travels'));
49
50 $scope.doRefresh = function() {
51 $http.get('http://localhost:3000/api/travels')
52 .success(function(data, status, headers,config){
53 console.log('data success');
54 console.log(data); // for browser console
55 $scope.travels = data; // for UI
56 localStorage.setItem('c_travels', JSON.stringify($scope.travels));
57 $scope.$broadcast('scroll.refreshComplete');//refresher stop
58 })
59 .error(function(data, status, headers,config){
60 console.log('data error');
61 $scope.$broadcast('scroll.refreshComplete');//refresher stop
62 })
63 .then(function(result){
64 travels = result.data;
65 });
66 };
67
68 $scope.newtravel={};
69
70 // Create the login modal that we will use later
71 $ionicModal.fromTemplateUrl('templates/newofferingtravel.html', {
72 scope: $scope
73 }).then(function(modal) {
74 $scope.modalOffering = modal;
75 });
76
77 // Create the login modal that we will use later
78 $ionicModal.fromTemplateUrl('templates/newaskingtravel.html', {
79 scope: $scope
80 }).then(function(modal) {
81 $scope.modalAsking = modal;
82 });
83
84 $ionicModal.fromTemplateUrl('templates/newaskingpackage.html', {
85 scope: $scope
86 }).then(function(modal) {
87 $scope.modalPackage = modal;
88 });
89
90 // Triggered in the login modal to close it
91 $scope.closeNewOfferingTravel = function() {
92 $scope.modalOffering.hide();
93 };
94 // Triggered in the login modal to close it
95 $scope.closeNewAskingTravel = function() {
96 $scope.modalAsking.hide();
97 };
98 $scope.closeNewAskingPackage = function() {
99 $scope.modalPackage.hide();
100 };
101
102 // Open the login modal
103 $scope.showNewOfferingTravel = function() {
104 $scope.modalOffering.show();
105 };
106 // Open the login modal
107 $scope.showNewAskingTravel = function() {
108 $scope.modalAsking.show();
109 };
110 $scope.showNewAskingPackage = function() {
111 $scope.modalPackage.show();
112 };
113
114 // Perform the login action when the user submits the login form
115 $scope.doNewOfferingTravel = function() {
116 console.log('Doing new travel', $scope.newtravel);
117 $scope.newtravel.icon="lorry";
118 $scope.newtravel.generateddate=$scope.newtravel.date;
119 $scope.newtravel.owner="user";
120
121 $scope.newtravel.modality="offering";
122 console.log($scope.newtravel);
123 $http({
124 url: 'http://localhost:3000/api/travels',
125 method: "POST",
126 data: $scope.newtravel
127 })
128 .then(function(response) {
129 // success
130 console.log("response: ");
131 console.log(response);
132 $scope.newtravel._id=response.data._id;
133 $scope.travels.push($scope.newtravel);
134 $scope.newtravel={};
135 },
136 function(response) { // optional
137 // failed
138 });
139
140 // Simulate a login delay. Remove this and replace with your login
141 // code if using a login system
142 $timeout(function() {
143 $scope.closeNewOfferingTravel();
144 }, 1000);
145 };
146
147 $scope.doNewAskingTravel = function() {
148 console.log('Doing new travel', $scope.newtravel);
149 $scope.newtravel.icon="lorry";
150 $scope.newtravel.generateddate=$scope.newtravel.date;
151 $scope.newtravel.owner="user";
152
153 $scope.newtravel.modality="asking";
154 console.log($scope.newtravel);
155 $http({
156 url: 'http://localhost:3000/api/travels',
157 method: "POST",
158 data: $scope.newtravel
159 })
160 .then(function(response) {
161 // success
162 console.log("response: ");
163 console.log(response);
164 $scope.newtravel._id=response.data._id;
165 $scope.travels.push($scope.newtravel);
166 },
167 function(response) { // optional
168 // failed
169 });
170
171 // Simulate a login delay. Remove this and replace with your login
172 // code if using a login system
173 $timeout(function() {
174 $scope.closeNewAskingTravel();
175 }, 1000);
176 };
177
178 $scope.doNewAskingPackage = function() {
179 console.log('Doing new package', $scope.newtravel);
180 $scope.newtravel.icon="lorry";
181 $scope.newtravel.generateddate=$scope.newtravel.date;
182 $scope.newtravel.owner="user";
183 $scope.newtravel.package=true;
184
185 $scope.newtravel.modality="package";
186 console.log($scope.newtravel);
187 $http({
188 url: 'http://localhost:3000/api/travels',
189 method: "POST",
190 data: $scope.newtravel
191 })
192 .then(function(response) {
193 // success
194 console.log("response: ");
195 console.log(response);
196 $scope.newtravel._id=response.data._id;
197 $scope.travels.push($scope.newtravel);
198 },
199 function(response) { // optional
200 // failed
201 });
202
203 // Simulate a login delay. Remove this and replace with your login
204 // code if using a login system
205 $timeout(function() {
206 $scope.closeNewAskingPackage();
207 }, 1000);
208 };
209})
210
211.controller('TravelCtrl', function($scope, $stateParams, $http) {
212 $scope.travel="";
213 console.log($stateParams.travelId);
214 $http.get('http://localhost:3000/api/travels/'+$stateParams.travelId)
215 .success(function(data, status, headers,config){
216 console.log('data success');
217 console.log(data); // for browser console
218 $scope.travel = data; // for UI
219 })
220 .error(function(data, status, headers,config){
221 console.log('data error');
222 })
223 .then(function(result){
224 travels = result.data;
225 });
226})
227
228.controller('UsersCtrl', function($scope, $http, $ionicModal, $timeout) {
229 $scope.users="";
230
231 $scope.users=JSON.parse(localStorage.getItem('c_users'));
232
233 $scope.doRefresh = function() {
234 $http.get('http://localhost:3000/api/users')
235 .success(function(data, status, headers, config){
236 console.log('data success');
237 console.log(data); // for browser console
238 $scope.users = data; // for UI
239 localStorage.setItem('c_users', JSON.stringify($scope.users));
240 $scope.$broadcast('scroll.refreshComplete');//refresher stop
241 })
242 .error(function(data, status, headers,config){
243 console.log('data error');
244 $scope.$broadcast('scroll.refreshComplete');//refresher stop
245 })
246 .then(function(result){
247 users = result.data;
248 });
249 };
250})
251
252.controller('UserCtrl', function($scope, $stateParams, $http) {
253 $scope.travel="";
254 console.log($stateParams.username);
255 $http.get('http://localhost:3000/api/users/byusername/'+$stateParams.username)
256 .success(function(data, status, headers,config){
257 console.log('data success');
258 console.log(data); // for browser console
259 $scope.user = data; // for UI
260 })
261 .error(function(data, status, headers,config){
262 console.log('data error');
263 })
264 .then(function(result){
265 travels = result.data;
266 });
267});