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