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.

179 lines
5.2 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.doRefresh = function() {
  38. $http.get('http://localhost:3000/api/travels')
  39. .success(function(data, status, headers,config){
  40. console.log('data success');
  41. console.log(data); // for browser console
  42. $scope.travels = data; // for UI
  43. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  44. })
  45. .error(function(data, status, headers,config){
  46. console.log('data error');
  47. $scope.$broadcast('scroll.refreshComplete');//refresher stop
  48. })
  49. .then(function(result){
  50. travels = result.data;
  51. });
  52. };
  53. $scope.newtravel={};
  54. // Create the login modal that we will use later
  55. $ionicModal.fromTemplateUrl('templates/newofferingtravel.html', {
  56. scope: $scope
  57. }).then(function(modal) {
  58. $scope.modalOffering = modal;
  59. });
  60. // Create the login modal that we will use later
  61. $ionicModal.fromTemplateUrl('templates/newaskingtravel.html', {
  62. scope: $scope
  63. }).then(function(modal) {
  64. $scope.modalAsking = modal;
  65. });
  66. // Triggered in the login modal to close it
  67. $scope.closeNewOfferingTravel = function() {
  68. $scope.modalOffering.hide();
  69. };
  70. // Triggered in the login modal to close it
  71. $scope.closeNewAskingTravel = function() {
  72. $scope.modalAsking.hide();
  73. };
  74. // Open the login modal
  75. $scope.showNewOfferingTravel = function() {
  76. $scope.modalOffering.show();
  77. };
  78. // Open the login modal
  79. $scope.showNewAskingTravel = function() {
  80. $scope.modalAsking.show();
  81. };
  82. // Perform the login action when the user submits the login form
  83. $scope.doNewOfferingTravel = function() {
  84. console.log('Doing new travel', $scope.newtravel);
  85. $scope.newtravel.icon="lorry";
  86. $scope.newtravel.generateddate=$scope.newtravel.date;
  87. $scope.newtravel.owner="user";
  88. $scope.newtravel.modality="offering";
  89. console.log($scope.newtravel);
  90. $http({
  91. url: 'http://localhost:3000/api/travels',
  92. method: "POST",
  93. data: $scope.newtravel
  94. })
  95. .then(function(response) {
  96. // success
  97. console.log("response: ");
  98. console.log(response);
  99. $scope.newtravel._id=response.data._id;
  100. $scope.travels.push($scope.newtravel);
  101. },
  102. function(response) { // optional
  103. // failed
  104. });
  105. // Simulate a login delay. Remove this and replace with your login
  106. // code if using a login system
  107. $timeout(function() {
  108. $scope.closeNewOfferingTravel();
  109. }, 1000);
  110. };
  111. $scope.doNewAskingTravel = function() {
  112. console.log('Doing new travel', $scope.newtravel);
  113. $scope.newtravel.icon="lorry";
  114. $scope.newtravel.generateddate=$scope.newtravel.date;
  115. $scope.newtravel.owner="user";
  116. $scope.newtravel.modality="asking";
  117. console.log($scope.newtravel);
  118. $http({
  119. url: 'http://localhost:3000/api/travels',
  120. method: "POST",
  121. data: $scope.newtravel
  122. })
  123. .then(function(response) {
  124. // success
  125. console.log("response: ");
  126. console.log(response);
  127. $scope.newtravel._id=response.data._id;
  128. $scope.travels.push($scope.newtravel);
  129. },
  130. function(response) { // optional
  131. // failed
  132. });
  133. // Simulate a login delay. Remove this and replace with your login
  134. // code if using a login system
  135. $timeout(function() {
  136. $scope.closeNewAskingTravel();
  137. }, 1000);
  138. };
  139. })
  140. .controller('TravelCtrl', function($scope, $stateParams, $http) {
  141. $scope.travel="";
  142. console.log($stateParams.travelId);
  143. $http.get('http://localhost:3000/api/travels/'+$stateParams.travelId)
  144. .success(function(data, status, headers,config){
  145. console.log('data success');
  146. console.log(data); // for browser console
  147. $scope.travel = data; // for UI
  148. })
  149. .error(function(data, status, headers,config){
  150. console.log('data error');
  151. })
  152. .then(function(result){
  153. travels = result.data;
  154. });
  155. });