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    $http.get('http://localhost:3000/api/travels')
 48    .success(function(data, status, headers,config){
 49        console.log('data success');
 50        console.log(data); // for browser console
 51        $scope.travels = data; // for UI
 52    })
 53    .error(function(data, status, headers,config){
 54        console.log('data error');
 55    })
 56    .then(function(result){
 57        travels = result.data;
 58    });
 59
 60
 61    $scope.newtravel={};
 62
 63  // Create the login modal that we will use later
 64  $ionicModal.fromTemplateUrl('templates/newtravel.html', {
 65    scope: $scope
 66  }).then(function(modal) {
 67    $scope.modal = modal;
 68  });
 69  // Triggered in the login modal to close it
 70  $scope.closeNewTravel = function() {
 71    $scope.modal.hide();
 72  };
 73
 74  // Open the login modal
 75  $scope.showNewTravel = function() {
 76    $scope.modal.show();
 77  };
 78  // Perform the login action when the user submits the login form
 79  $scope.doNewTravel = 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    console.log($scope.newtravel);
 85    $http({
 86        url: 'http://localhost:3000/api/travels',
 87        method: "POST",
 88        data: $scope.newtravel
 89    })
 90    .then(function(response) {
 91            // success
 92            console.log("response: ");
 93            console.log(response);
 94            $scope.newtravel._id=response.data._id;
 95            $scope.travels.push($scope.newtravel);
 96    },
 97    function(response) { // optional
 98            // failed
 99    });
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.closeNewTravel();
105    }, 1000);
106  };
107})
108
109.controller('TravelCtrl', function($scope, $stateParams, $http) {
110    $scope.travel="";
111    console.log($stateParams.travelId);
112    $http.get('http://localhost:3000/api/travels/'+$stateParams.travelId)
113        .success(function(data, status, headers,config){
114            console.log('data success');
115            console.log(data); // for browser console
116            $scope.travel = data; // for UI
117        })
118        .error(function(data, status, headers,config){
119            console.log('data error');
120        })
121        .then(function(result){
122            travels = result.data;
123    });
124});