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/newofferingtravel.html', {
65 scope: $scope
66 }).then(function(modal) {
67 $scope.modalOffering = modal;
68 });
69
70 // Create the login modal that we will use later
71 $ionicModal.fromTemplateUrl('templates/newaskingtravel.html', {
72 scope: $scope
73 }).then(function(modal) {
74 $scope.modalAsking = modal;
75 });
76
77 // Triggered in the login modal to close it
78 $scope.closeNewOfferingTravel = function() {
79 $scope.modalOffering.hide();
80 };
81 // Triggered in the login modal to close it
82 $scope.closeNewAskingTravel = function() {
83 $scope.modalAsking.hide();
84 };
85
86 // Open the login modal
87 $scope.showNewOfferingTravel = function() {
88 $scope.modalOffering.show();
89 };
90 // Open the login modal
91 $scope.showNewAskingTravel = function() {
92 $scope.modalAsking.show();
93 };
94
95 // Perform the login action when the user submits the login form
96 $scope.doNewOfferingTravel = function() {
97 console.log('Doing new travel', $scope.newtravel);
98 $scope.newtravel.icon="lorry";
99 $scope.newtravel.generateddate=$scope.newtravel.date;
100 $scope.newtravel.owner="user";
101
102 $scope.newtravel.modality="offering";
103 console.log($scope.newtravel);
104 $http({
105 url: 'http://localhost:3000/api/travels',
106 method: "POST",
107 data: $scope.newtravel
108 })
109 .then(function(response) {
110 // success
111 console.log("response: ");
112 console.log(response);
113 $scope.newtravel._id=response.data._id;
114 $scope.travels.push($scope.newtravel);
115 },
116 function(response) { // optional
117 // failed
118 });
119
120 // Simulate a login delay. Remove this and replace with your login
121 // code if using a login system
122 $timeout(function() {
123 $scope.closeNewOfferingTravel();
124 }, 1000);
125 };
126})
127
128.controller('TravelCtrl', function($scope, $stateParams, $http) {
129 $scope.travel="";
130 console.log($stateParams.travelId);
131 $http.get('http://localhost:3000/api/travels/'+$stateParams.travelId)
132 .success(function(data, status, headers,config){
133 console.log('data success');
134 console.log(data); // for browser console
135 $scope.travel = data; // for UI
136 })
137 .error(function(data, status, headers,config){
138 console.log('data error');
139 })
140 .then(function(result){
141 travels = result.data;
142 });
143});