From: Gustavo Martin Morcuende Date: Tue, 25 Aug 2015 19:19:28 +0000 (+0200) Subject: showcase: AngularJS REST service, API CARS X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=84c5a35c10b51232a6ea2a632188e9476f8db470;p=JavaScriptForFun showcase: AngularJS REST service, API CARS --- diff --git a/angularjs/showcase/src/showcase/app/rest/rest.constants.js b/angularjs/showcase/src/showcase/app/rest/rest.constants.js new file mode 100644 index 0000000..a82540f --- /dev/null +++ b/angularjs/showcase/src/showcase/app/rest/rest.constants.js @@ -0,0 +1,13 @@ +(function () { + 'use strict'; + + var prefix = 'http://localhost:8080/'; + + angular + .module('app.rest') + .constant('API', { + CARS: prefix + 'api/cars', + CAR: prefix + 'api/cars/:carId' + }); + +})(); diff --git a/angularjs/showcase/src/showcase/app/rest/rest.service.js b/angularjs/showcase/src/showcase/app/rest/rest.service.js index e2eef11..618eb82 100644 --- a/angularjs/showcase/src/showcase/app/rest/rest.service.js +++ b/angularjs/showcase/src/showcase/app/rest/rest.service.js @@ -22,7 +22,7 @@ * Rest service. */ /* @ngInject */ - function cars($http, $log) { + function cars($http, $log, API) { return { getAll: getAll }; @@ -36,7 +36,7 @@ * Get cars from API REST. */ function getAll() { - return $http.get('/api/cars') + return $http.get(API.CARS) .then(getAllCompleted) .catch(getAllFailed); diff --git a/angularjs/showcase/src/showcase/app/rest/rest.service.spec.js b/angularjs/showcase/src/showcase/app/rest/rest.service.spec.js index 8fa8d70..7449f84 100644 --- a/angularjs/showcase/src/showcase/app/rest/rest.service.spec.js +++ b/angularjs/showcase/src/showcase/app/rest/rest.service.spec.js @@ -4,21 +4,23 @@ describe('app.rest', function() { var $httpBackend; var $log; var cars; + var API; beforeEach(function() { module('app.rest'); - inject(function(_$httpBackend_, _$log_, _cars_) { + inject(function(_$httpBackend_, _$log_, _cars_, _API_) { $httpBackend = _$httpBackend_; $log = _$log_; cars = _cars_; + API = _API_; }); }); describe('cars service', function () { it('should invoke GET all cars', function () { - $httpBackend.expectGET('/api/cars').respond({}); + $httpBackend.expectGET(API.CARS).respond({}); cars.getAll(); diff --git a/angularjs/showcase/src/showcase/index.html b/angularjs/showcase/src/showcase/index.html index 2b3c913..4c19879 100644 --- a/angularjs/showcase/src/showcase/index.html +++ b/angularjs/showcase/src/showcase/index.html @@ -58,6 +58,7 @@ +