showcase: having fun with promises and their states
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 8 Sep 2015 23:38:30 +0000 (01:38 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 8 Sep 2015 23:38:30 +0000 (01:38 +0200)
angularjs/showcase/src/showcase/app/cars/cars.controller.js
angularjs/showcase/src/showcase/app/cars/cars.html
angularjs/showcase/src/showcase/app/cars/cars.service.js

index 1f47e58..7734c20 100644 (file)
         }
       );
     };
+    vm.getCar = function() {
+
+      console.log('getCar: START');
+
+      cars.getById(1).then(
+        function (value) {
+          console.log('getCar getById: Cars controller value: ' + JSON.stringify(value));
+        },
+        function(reason) {
+          console.log('getCar getById: Cars controller reason: ' + JSON.stringify(reason));
+        }
+      );
+
+      cars.getExpectedValue().then(
+        function (value) {
+          console.log('getCar getExpectedValue: Cars controller value: ' + JSON.stringify(value));
+        },
+        function(reason) {
+          console.log('getCar getExpectedValue: Cars controller reason: ' + JSON.stringify(reason));
+        }
+      );
+
+      console.log('getCar: END');
+    };
+    vm.getResolvedPromise = function() {
+      console.log('getResolvedPromise: START');
+
+      cars.getExpectedValue().then(
+        function (value) {
+          console.log('getResolvedPromise getExpectedValue: Cars controller value: ' + JSON.stringify(value));
+        },
+        function(reason) {
+          console.log('getResolvedPromise getExpectedValue: Cars controller reason: ' + JSON.stringify(reason));
+        }
+      );
+
+      console.log('getResolvedPromise: END');
+    };
 
     // How to test "private" methods in controllers?
     // Two options:
index 812733d..fda95e9 100644 (file)
             ng-click="vm.getCars()">
       Get cars
     </button>
+    <button type="button" class="btn btn-primary" ng-disabled="myForm.input.$invalid"
+            ng-click="vm.getCar()">
+      Get car
+    </button>
+    <button type="button" class="btn btn-primary" ng-disabled="myForm.input.$invalid"
+            ng-click="vm.getResolvedPromise()">
+      Get resolved promise
+    </button>
   </form>
 
   <label>controller as directive:</label>
index 79e721e..0a25ea3 100644 (file)
    */
   /* @ngInject */
   function cars($http, $q, API) {
+    var expectedValue;
+
     return {
-      getAll: getAll
+      getAll: getAll,
+      getById: getById,
+      getExpectedValue: getExpectedValue
     };
 
     /**
         console.log('XHR Finalizer for getAll.');
       }
     }
+
+    /**
+     * @ngdoc method
+     * @name  getById
+     * @methodOf app.cars.cars
+     *
+     * @description
+     * Get one car by its id from API REST.
+     */
+    function getById(id) {
+      expectedValue = $http.get(API.CAR.replace(':carId', id));
+
+      return expectedValue;
+    }
+
+    function getExpectedValue() {
+      return expectedValue;
+    }
   }
 
 }());