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 // Triggered in the login modal to close it
85 $scope.closeNewOfferingTravel = function() {
86 $scope.modalOffering.hide();
87 };
88 // Triggered in the login modal to close it
89 $scope.closeNewAskingTravel = function() {
90 $scope.modalAsking.hide();
91 };
92
93 // Open the login modal
94 $scope.showNewOfferingTravel = function() {
95 $scope.modalOffering.show();
96 };
97 // Open the login modal
98 $scope.showNewAskingTravel = function() {
99 $scope.modalAsking.show();
100 };
101
102 // Perform the login action when the user submits the login form
103 $scope.doNewOfferingTravel = function() {
104 console.log('Doing new travel', $scope.newtravel);
105 $scope.newtravel.icon="lorry";
106 $scope.newtravel.generateddate=$scope.newtravel.date;
107 $scope.newtravel.owner="user";
108
109 $scope.newtravel.modality="offering";
110 console.log($scope.newtravel);
111 $http({
112 url: 'http://localhost:3000/api/travels',
113 method: "POST",
114 data: $scope.newtravel
115 })
116 .then(function(response) {
117 // success
118 console.log("response: ");
119 console.log(response);
120 $scope.newtravel._id=response.data._id;
121 $scope.travels.push($scope.newtravel);
122 $scope.newtravel={};
123 },
124 function(response) { // optional
125 // failed
126 });
127
128 // Simulate a login delay. Remove this and replace with your login
129 // code if using a login system
130 $timeout(function() {
131 $scope.closeNewOfferingTravel();
132 }, 1000);
133 };
134
135 $scope.doNewAskingTravel = function() {
136 console.log('Doing new travel', $scope.newtravel);
137 $scope.newtravel.icon="lorry";
138 $scope.newtravel.generateddate=$scope.newtravel.date;
139 $scope.newtravel.owner="user";
140
141 $scope.newtravel.modality="asking";
142 console.log($scope.newtravel);
143 $http({
144 url: 'http://localhost:3000/api/travels',
145 method: "POST",
146 data: $scope.newtravel
147 })
148 .then(function(response) {
149 // success
150 console.log("response: ");
151 console.log(response);
152 $scope.newtravel._id=response.data._id;
153 $scope.travels.push($scope.newtravel);
154 },
155 function(response) { // optional
156 // failed
157 });
158
159 // Simulate a login delay. Remove this and replace with your login
160 // code if using a login system
161 $timeout(function() {
162 $scope.closeNewAskingTravel();
163 }, 1000);
164 };
165})
166
167.controller('TravelCtrl', function($scope, $stateParams, $http) {
168 $scope.travel="";
169 console.log($stateParams.travelId);
170 $http.get('http://localhost:3000/api/travels/'+$stateParams.travelId)
171 .success(function(data, status, headers,config){
172 console.log('data success');
173 console.log(data); // for browser console
174 $scope.travel = data; // for UI
175 })
176 .error(function(data, status, headers,config){
177 console.log('data error');
178 })
179 .then(function(result){
180 travels = result.data;
181 });
182})
183
184.controller('UsersCtrl', function($scope, $http, $ionicModal, $timeout) {
185 $scope.users="";
186
187 $scope.users=JSON.parse(localStorage.getItem('c_users'));
188
189 $scope.doRefresh = function() {
190 $http.get('http://localhost:3000/api/users')
191 .success(function(data, status, headers, config){
192 console.log('data success');
193 console.log(data); // for browser console
194 $scope.users = data; // for UI
195 localStorage.setItem('c_users', JSON.stringify($scope.users));
196 $scope.$broadcast('scroll.refreshComplete');//refresher stop
197 })
198 .error(function(data, status, headers,config){
199 console.log('data error');
200 $scope.$broadcast('scroll.refreshComplete');//refresher stop
201 })
202 .then(function(result){
203 users = result.data;
204 });
205 };
206})
207
208.controller('UserCtrl', function($scope, $stateParams, $http) {
209 $scope.travel="";
210 console.log($stateParams.username);
211 $http.get('http://localhost:3000/api/users/byusername/'+$stateParams.username)
212 .success(function(data, status, headers,config){
213 console.log('data success');
214 console.log(data); // for browser console
215 $scope.user = data; // for UI
216 })
217 .error(function(data, status, headers,config){
218 console.log('data error');
219 })
220 .then(function(result){
221 travels = result.data;
222 });
223});