EVOLUTION-MANAGER
Edit File: step_11.html
<a href='https://github.com/angular/angular.js/edit/v1.3.x/docs/content/tutorial/step_11.ngdoc?message=docs(tutorial%2F11 - REST and Custom Services)%3A%20describe%20your%20change...' class='improve-docs btn btn-primary'><i class="glyphicon glyphicon-edit"> </i>Improve this Doc</a> <ul doc-tutorial-nav="11"></ul> <p>In this step, you will change the way our app fetches data.</p> <ul> <li>We define a custom service that represents a <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">RESTful</a> client. Using this client we can make requests to the server for data in an easier way, without having to deal with the lower-level <a href="api/ng/service/$http">$http</a> API, HTTP methods and URLs.</li> </ul> <div doc-tutorial-reset="11"></div> <h2 id="dependencies">Dependencies</h2> <p>The RESTful functionality is provided by Angular in the <code>ngResource</code> module, which is distributed separately from the core Angular framework.</p> <p>We are using <a href="http://bower.io/">Bower</a> to install client side dependencies. This step updates the <code>bower.json</code> configuration file to include the new dependency:</p> <pre><code>{ "name": "angular-seed", "description": "A starter project for AngularJS", "version": "0.0.0", "homepage": "https://github.com/angular/angular-seed", "license": "MIT", "private": true, "dependencies": { "angular": "~1.3.0", "angular-mocks": "~1.3.0", "bootstrap": "~3.1.1", "angular-route": "~1.3.0", "angular-resource": "~1.3.0" } } </code></pre> <p>The new dependency <code>"angular-resource": "~1.3.0"</code> tells bower to install a version of the angular-resource component that is compatible with version 1.3.x. We must ask bower to download and install this dependency. We can do this by running:</p> <pre><code>npm install </code></pre> <div class="alert alert-warning"> <strong>Warning:</strong> If a new version of Angular has been released since you last ran <code>npm install</code>, then you may have a problem with the <code>bower install</code> due to a conflict between the versions of angular.js that need to be installed. If you get this then simply delete your <code>app/bower_components</code> folder before running <code>npm install</code>. </div> <div class="alert alert-info"> <strong>Note:</strong> If you have bower installed globally then you can run <code>bower install</code> but for this project we have preconfigured <code>npm install</code> to run bower for us. </div> <h2 id="template">Template</h2> <p>Our custom resource service will be defined in <code>app/js/services.js</code> so we need to include this file in our layout template. Additionally, we also need to load the <code>angular-resource.js</code> file, which contains the <a href="api/ngResource">ngResource</a> module:</p> <p><strong><code>app/index.html</code>.</strong></p> <pre><code class="lang-html">... <script src="bower_components/angular-resource/angular-resource.js"></script> <script src="js/services.js"></script> ... </code></pre> <h2 id="service">Service</h2> <p>We create our own service to provide access to the phone data on the server:</p> <p><strong><code>app/js/services.js</code>.</strong></p> <pre><code class="lang-js">var phonecatServices = angular.module('phonecatServices', ['ngResource']); phonecatServices.factory('Phone', ['$resource', function($resource){ return $resource('phones/:phoneId.json', {}, { query: {method:'GET', params:{phoneId:'phones'}, isArray:true} }); }]); </code></pre> <p>We used the module API to register a custom service using a factory function. We passed in the name of the service - 'Phone' - and the factory function. The factory function is similar to a controller's constructor in that both can declare dependencies to be injected via function arguments. The Phone service declared a dependency on the <code>$resource</code> service.</p> <p>The <a href="api/ngResource/service/$resource"><code>$resource</code></a> service makes it easy to create a <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">RESTful</a> client with just a few lines of code. This client can then be used in our application, instead of the lower-level <a href="api/ng/service/$http">$http</a> service.</p> <p><strong><code>app/js/app.js</code>.</strong></p> <pre><code class="lang-js">... angular.module('phonecatApp', ['ngRoute', 'phonecatControllers','phonecatFilters', 'phonecatServices']). ... </code></pre> <p>We need to add the 'phonecatServices' module dependency to 'phonecatApp' module's requires array.</p> <h2 id="controller">Controller</h2> <p>We simplified our sub-controllers (<code>PhoneListCtrl</code> and <code>PhoneDetailCtrl</code>) by factoring out the lower-level <a href="api/ng/service/$http">$http</a> service, replacing it with a new service called <code>Phone</code>. Angular's <a href="api/ngResource/service/$resource"><code>$resource</code></a> service is easier to use than <code>$http</code> for interacting with data sources exposed as RESTful resources. It is also easier now to understand what the code in our controllers is doing.</p> <p><strong><code>app/js/controllers.js</code>.</strong></p> <pre><code class="lang-js">var phonecatControllers = angular.module('phonecatControllers', []); ... phonecatControllers.controller('PhoneListCtrl', ['$scope', 'Phone', function($scope, Phone) { $scope.phones = Phone.query(); $scope.orderProp = 'age'; }]); phonecatControllers.controller('PhoneDetailCtrl', ['$scope', '$routeParams', 'Phone', function($scope, $routeParams, Phone) { $scope.phone = Phone.get({phoneId: $routeParams.phoneId}, function(phone) { $scope.mainImageUrl = phone.images[0]; }); $scope.setImage = function(imageUrl) { $scope.mainImageUrl = imageUrl; } }]); </code></pre> <p>Notice how in <code>PhoneListCtrl</code> we replaced:</p> <pre><code>$http.get('phones/phones.json').success(function(data) { $scope.phones = data; }); </code></pre> <p>with:</p> <pre><code>$scope.phones = Phone.query(); </code></pre> <p>This is a simple statement that we want to query for all phones.</p> <p>An important thing to notice in the code above is that we don't pass any callback functions when invoking methods of our Phone service. Although it looks as if the result were returned synchronously, that is not the case at all. What is returned synchronously is a "future" — an object, which will be filled with data when the XHR response returns. Because of the data-binding in Angular, we can use this future and bind it to our template. Then, when the data arrives, the view will automatically update.</p> <p>Sometimes, relying on the future object and data-binding alone is not sufficient to do everything we require, so in these cases, we can add a callback to process the server response. The <code>PhoneDetailCtrl</code> controller illustrates this by setting the <code>mainImageUrl</code> in a callback.</p> <h2 id="test">Test</h2> <p>Because we're now using the <a href="api/ngResource">ngResource</a> module, it's necessary to update the Karma config file with angular-resource so the new tests will pass.</p> <p><strong><code>test/karma.conf.js</code>:</strong></p> <pre><code class="lang-js">files : [ 'app/bower_components/angular/angular.js', 'app/bower_components/angular-route/angular-route.js', 'app/bower_components/angular-resource/angular-resource.js', 'app/bower_components/angular-mocks/angular-mocks.js', 'app/js/**/*.js', 'test/unit/**/*.js' ], </code></pre> <p>We have modified our unit tests to verify that our new service is issuing HTTP requests and processing them as expected. The tests also check that our controllers are interacting with the service correctly.</p> <p>The <a href="api/ngResource/service/$resource">$resource</a> service augments the response object with methods for updating and deleting the resource. If we were to use the standard <code>toEqual</code> matcher, our tests would fail because the test values would not match the responses exactly. To solve the problem, we use a newly-defined <code>toEqualData</code> <a href="https://github.com/pivotal/jasmine/wiki/Matchers">Jasmine matcher</a>. When the <code>toEqualData</code> matcher compares two objects, it takes only object properties into account and ignores methods.</p> <p><strong><code>test/unit/controllersSpec.js</code>:</strong></p> <pre><code class="lang-js">describe('PhoneCat controllers', function() { beforeEach(function(){ this.addMatchers({ toEqualData: function(expected) { return angular.equals(this.actual, expected); } }); }); beforeEach(module('phonecatApp')); beforeEach(module('phonecatServices')); describe('PhoneListCtrl', function(){ var scope, ctrl, $httpBackend; beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) { $httpBackend = _$httpBackend_; $httpBackend.expectGET('phones/phones.json'). respond([{name: 'Nexus S'}, {name: 'Motorola DROID'}]); scope = $rootScope.$new(); ctrl = $controller('PhoneListCtrl', {$scope: scope}); })); it('should create "phones" model with 2 phones fetched from xhr', function() { expect(scope.phones).toEqualData([]); $httpBackend.flush(); expect(scope.phones).toEqualData( [{name: 'Nexus S'}, {name: 'Motorola DROID'}]); }); it('should set the default value of orderProp model', function() { expect(scope.orderProp).toBe('age'); }); }); describe('PhoneDetailCtrl', function(){ var scope, $httpBackend, ctrl, xyzPhoneData = function() { return { name: 'phone xyz', images: ['image/url1.png', 'image/url2.png'] } }; beforeEach(inject(function(_$httpBackend_, $rootScope, $routeParams, $controller) { $httpBackend = _$httpBackend_; $httpBackend.expectGET('phones/xyz.json').respond(xyzPhoneData()); $routeParams.phoneId = 'xyz'; scope = $rootScope.$new(); ctrl = $controller('PhoneDetailCtrl', {$scope: scope}); })); it('should fetch phone detail', function() { expect(scope.phone).toEqualData({}); $httpBackend.flush(); expect(scope.phone).toEqualData(xyzPhoneData()); }); }); }); </code></pre> <p>You should now see the following output in the Karma tab:</p> <pre>Chrome 22.0: Executed 5 of 5 SUCCESS (0.038 secs / 0.01 secs)</pre> <h1 id="summary">Summary</h1> <p>Now that we've seen how to build a custom service as a RESTful client, we're ready for <a href="tutorial/step_12">step 12</a> (the last step!) to learn how to improve this application with animations.</p> <ul doc-tutorial-nav="11"></ul>