From: Gustavo Martin Morcuende Date: Tue, 25 Aug 2015 06:31:14 +0000 (+0200) Subject: showcase: REST web services X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=55e986f958b86c9d36fda97fb6a10b23fb44a0ee;p=JavaScriptForFun showcase: REST web services --- diff --git a/angularjs/showcase/src/showcase/app/app.module.js b/angularjs/showcase/src/showcase/app/app.module.js index 4e68a30..e20f31d 100644 --- a/angularjs/showcase/src/showcase/app/app.module.js +++ b/angularjs/showcase/src/showcase/app/app.module.js @@ -8,6 +8,7 @@ * * @requires app.core * @requires app.welcome + * @requires app.rest * * @description * # app @@ -20,7 +21,8 @@ 'app.core', /* Feature areas */ - 'app.welcome' + 'app.welcome', + 'app.rest' ]); }()); diff --git a/angularjs/showcase/src/showcase/app/rest/rest.controller.js b/angularjs/showcase/src/showcase/app/rest/rest.controller.js new file mode 100644 index 0000000..66f6956 --- /dev/null +++ b/angularjs/showcase/src/showcase/app/rest/rest.controller.js @@ -0,0 +1,32 @@ +(function () { + 'use strict'; + + angular + .module('app.rest') + .controller('Rest', Rest); + + /** + * @ngdoc controller + * @name app.rest.controller:Rest + * + * @requires $location + * + *

+ *
+ * {@link https://docs.angularjs.org/api/ng/service/$location $location} + *

+ * + * @description + * Rest controller. + */ + /* @ngInject */ + function Rest($location) { + var vm = this; + vm.example = { + text: 'try to send data', + word: /^\s*\w*\s*$/, + singleModel: 1 + }; + } + +})(); diff --git a/angularjs/showcase/src/showcase/app/rest/rest.html b/angularjs/showcase/src/showcase/app/rest/rest.html new file mode 100644 index 0000000..c34fdb4 --- /dev/null +++ b/angularjs/showcase/src/showcase/app/rest/rest.html @@ -0,0 +1,27 @@ + +
+ +
+ + +
+ + Required! + + Single word only! +
+ text = {{vm.example.text}}
+ myForm.input.$valid = {{myForm.input.$valid}}
+ myForm.input.$error = {{myForm.input.$error}}
+ myForm.$valid = {{myForm.$valid}}
+ myForm.$error.required = {{!!myForm.$error.required}}
+ +
+ + + + +
\ No newline at end of file diff --git a/angularjs/showcase/src/showcase/app/rest/rest.module.js b/angularjs/showcase/src/showcase/app/rest/rest.module.js new file mode 100644 index 0000000..284a218 --- /dev/null +++ b/angularjs/showcase/src/showcase/app/rest/rest.module.js @@ -0,0 +1,20 @@ +(function() { + 'use strict'; + + /** + * @ngdoc overview + * @name app.rest + * + * @requires app.core + * + * @description + * # app.welcome + * + * ## Module welcome. + * Module in charge of sending REST requests. + */ + angular.module('app.rest', [ + 'app.core' + ]); + +})(); diff --git a/angularjs/showcase/src/showcase/app/rest/rest.route.js b/angularjs/showcase/src/showcase/app/rest/rest.route.js new file mode 100644 index 0000000..5add5b9 --- /dev/null +++ b/angularjs/showcase/src/showcase/app/rest/rest.route.js @@ -0,0 +1,37 @@ +(function() { + 'use strict'; + + angular + .module('app.rest') + .config(configure); + + /** + * @ngdoc service + * @name app.rest.configure + * + * @requires $stateProvider + * @requires $urlRouterProvider + * + *

+ *
+ * {@link http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$stateProvider $stateProvider}
+ * {@link http://angular-ui.github.io/ui-router/site/#/api/ui.router.router.$urlRouterProvider $urlRouterProvider} + *

+ * + * + * @description + * Router configuration for rest application. + */ + /* @ngInject */ + function configure($stateProvider, $urlRouterProvider) { + var state = 'rest'; + var config = { + abstract: false, + url: '/rest', + templateUrl: 'app/rest/rest.html' + }; + + $urlRouterProvider.otherwise(state); + $stateProvider.state(state, config); + } +}()); diff --git a/angularjs/showcase/src/showcase/app/rest/rest.route.spec.js b/angularjs/showcase/src/showcase/app/rest/rest.route.spec.js new file mode 100644 index 0000000..91459f7 --- /dev/null +++ b/angularjs/showcase/src/showcase/app/rest/rest.route.spec.js @@ -0,0 +1,24 @@ +describe('app.rest', function() { + 'use strict'; + + describe('state', function() { + var view = { + rest: 'app/rest/rest.html' + }; + var $state; + + beforeEach(function() { + module('app.rest'); + + inject(function(_$state_) { + $state = _$state_; + }); + }); + + it('should map /rest route to rest View template', function() { + expect($state.get('rest').templateUrl). toEqual(view.rest); + }); + + }); + +}); diff --git a/angularjs/showcase/src/showcase/app/rest/rest.service.js b/angularjs/showcase/src/showcase/app/rest/rest.service.js new file mode 100644 index 0000000..e2eef11 --- /dev/null +++ b/angularjs/showcase/src/showcase/app/rest/rest.service.js @@ -0,0 +1,53 @@ +(function () { + 'use strict'; + + angular + .module('app.rest') + .factory('cars', cars); + + /** + * @ngdoc service + * @name app.rest.cars + * + * @requires $http + * @requires $log + * + *

+ *
+ * {@link https://docs.angularjs.org/api/ng/service/$http $http}
+ * {@link https://docs.angularjs.org/api/ng/service/$log $log} + *

+ * + * @description + * Rest service. + */ + /* @ngInject */ + function cars($http, $log) { + return { + getAll: getAll + }; + + /** + * @ngdoc method + * @name getAll + * @methodOf app.rest.cars + * + * @description + * Get cars from API REST. + */ + function getAll() { + return $http.get('/api/cars') + .then(getAllCompleted) + .catch(getAllFailed); + + function getAllCompleted(response) { + return response.data.results; + } + + function getAllFailed(error) { + $log.debug('XHR Failed for sendData.' + error.data); + } + } + } + +}()); diff --git a/angularjs/showcase/src/showcase/app/rest/rest.service.spec.js b/angularjs/showcase/src/showcase/app/rest/rest.service.spec.js new file mode 100644 index 0000000..8fa8d70 --- /dev/null +++ b/angularjs/showcase/src/showcase/app/rest/rest.service.spec.js @@ -0,0 +1,34 @@ +describe('app.rest', function() { + 'use strict'; + + var $httpBackend; + var $log; + var cars; + + beforeEach(function() { + module('app.rest'); + + inject(function(_$httpBackend_, _$log_, _cars_) { + $httpBackend = _$httpBackend_; + $log = _$log_; + cars = _cars_; + }); + }); + + describe('cars service', function () { + + it('should invoke GET all cars', function () { + $httpBackend.expectGET('/api/cars').respond({}); + + cars.getAll(); + + $httpBackend.flush(); + }); + }); + + afterEach(function() { + $httpBackend.verifyNoOutstandingExpectation(); + $httpBackend.verifyNoOutstandingRequest(); + }); + +}); diff --git a/angularjs/showcase/src/showcase/app/welcome/welcome.html b/angularjs/showcase/src/showcase/app/welcome/welcome.html index 6c05370..f47e20f 100644 --- a/angularjs/showcase/src/showcase/app/welcome/welcome.html +++ b/angularjs/showcase/src/showcase/app/welcome/welcome.html @@ -1,7 +1,7 @@ -
+
- {{ welcome.hello }} + {{ vm.hello }}
diff --git a/angularjs/showcase/src/showcase/app/welcome/welcome.module.js b/angularjs/showcase/src/showcase/app/welcome/welcome.module.js index 2115c9f..1498239 100644 --- a/angularjs/showcase/src/showcase/app/welcome/welcome.module.js +++ b/angularjs/showcase/src/showcase/app/welcome/welcome.module.js @@ -11,7 +11,7 @@ * # app.welcome * * ## Module welcome. - * Module in charge of displaying a nice welcome page. LOL ROLF + * Module in charge of displaying a nice welcome page. */ angular.module('app.welcome', [ 'app.core' diff --git a/angularjs/showcase/src/showcase/app/welcome/welcome.route.spec.js b/angularjs/showcase/src/showcase/app/welcome/welcome.route.spec.js index 4db3d48..7478589 100644 --- a/angularjs/showcase/src/showcase/app/welcome/welcome.route.spec.js +++ b/angularjs/showcase/src/showcase/app/welcome/welcome.route.spec.js @@ -16,7 +16,7 @@ describe('app.welcome', function() { }); it('should map /welcome route to welcome View template', function() { - expect($state.get('welcome').templateUrl). toEqual(view.welcome); + expect($state.get('welcome').templateUrl).toEqual(view.welcome); }); }); diff --git a/angularjs/showcase/src/showcase/index.html b/angularjs/showcase/src/showcase/index.html index 1d74680..2b3c913 100644 --- a/angularjs/showcase/src/showcase/index.html +++ b/angularjs/showcase/src/showcase/index.html @@ -54,6 +54,10 @@ + + + +