From: Gustavo Martin Morcuende Date: Tue, 8 Sep 2015 23:38:30 +0000 (+0200) Subject: showcase: having fun with promises and their states X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=ee9f818b18022b98b185cc1938a0302381c89329;p=JavaScriptForFun showcase: having fun with promises and their states --- diff --git a/angularjs/showcase/src/showcase/app/cars/cars.controller.js b/angularjs/showcase/src/showcase/app/cars/cars.controller.js index 1f47e58..7734c20 100644 --- a/angularjs/showcase/src/showcase/app/cars/cars.controller.js +++ b/angularjs/showcase/src/showcase/app/cars/cars.controller.js @@ -45,6 +45,44 @@ } ); }; + 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: diff --git a/angularjs/showcase/src/showcase/app/cars/cars.html b/angularjs/showcase/src/showcase/app/cars/cars.html index 812733d..fda95e9 100644 --- a/angularjs/showcase/src/showcase/app/cars/cars.html +++ b/angularjs/showcase/src/showcase/app/cars/cars.html @@ -26,6 +26,14 @@ ng-click="vm.getCars()"> Get cars + + diff --git a/angularjs/showcase/src/showcase/app/cars/cars.service.js b/angularjs/showcase/src/showcase/app/cars/cars.service.js index 79e721e..0a25ea3 100644 --- a/angularjs/showcase/src/showcase/app/cars/cars.service.js +++ b/angularjs/showcase/src/showcase/app/cars/cars.service.js @@ -23,8 +23,12 @@ */ /* @ngInject */ function cars($http, $q, API) { + var expectedValue; + return { - getAll: getAll + getAll: getAll, + getById: getById, + getExpectedValue: getExpectedValue }; /** @@ -193,6 +197,24 @@ 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; + } } }());