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.

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