You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
6.9 KiB

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