Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
step-11 custom service and $resource
Browse files Browse the repository at this point in the history
- Replaced [$http] with [$resource]
- Created a custom Phone service that represents the $resource client
  • Loading branch information
petebacondarwin committed Jan 20, 2016
1 parent 74fca65 commit 3a69cd6
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 21 deletions.
2 changes: 2 additions & 0 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
<link rel="stylesheet" href="css/app.css">
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="bower_components/angular-resource/angular-resource.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/filters.js"></script>
<script src="js/services.js"></script>
</head>
<body>

Expand Down
3 changes: 2 additions & 1 deletion app/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
var phonecatApp = angular.module('phonecatApp', [
'ngRoute',
'phonecatControllers',
'phonecatFilters'
'phonecatFilters',
'phonecatServices'
]);

phonecatApp.config(['$routeProvider',
Expand Down
18 changes: 7 additions & 11 deletions app/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@

var phonecatControllers = angular.module('phonecatControllers', []);

phonecatControllers.controller('PhoneListCtrl', ['$scope', '$http',
function($scope, $http) {
$http.get('phones/phones.json').success(function(data) {
$scope.phones = data;
});

phonecatControllers.controller('PhoneListCtrl', ['$scope', 'Phone',
function($scope, Phone) {
$scope.phones = Phone.query();
$scope.orderProp = 'age';
}]);

phonecatControllers.controller('PhoneDetailCtrl', ['$scope', '$routeParams', '$http',
function($scope, $routeParams, $http) {
$http.get('phones/' + $routeParams.phoneId + '.json').success(function(data) {
$scope.phone = data;
$scope.mainImageUrl = data.images[0];
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) {
Expand Down
8 changes: 8 additions & 0 deletions app/js/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@

/* Services */

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}
});
}]);
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"angular-mocks": "1.4.x",
"jquery": "~2.1.1",
"bootstrap": "~3.1.1",
"angular-route": "1.4.x"
"angular-route": "1.4.x",
"angular-resource": "1.4.x"
}
}
1 change: 1 addition & 0 deletions test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = function(config){
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'
Expand Down
19 changes: 14 additions & 5 deletions test/unit/controllersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
/* jasmine specs for controllers go here */
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;
Expand All @@ -19,11 +28,11 @@ describe('PhoneCat controllers', function() {


it('should create "phones" model with 2 phones fetched from xhr', function() {
expect(scope.phones).toBeUndefined();
expect(scope.phones).toEqualData([]);
$httpBackend.flush();

expect(scope.phones).toEqual([{name: 'Nexus S'},
{name: 'Motorola DROID'}]);
expect(scope.phones).toEqualData(
[{name: 'Nexus S'}, {name: 'Motorola DROID'}]);
});


Expand Down Expand Up @@ -54,10 +63,10 @@ describe('PhoneCat controllers', function() {


it('should fetch phone detail', function() {
expect(scope.phone).toBeUndefined();
expect(scope.phone).toEqualData({});
$httpBackend.flush();

expect(scope.phone).toEqual(xyzPhoneData());
expect(scope.phone).toEqualData(xyzPhoneData());
});
});
});
11 changes: 8 additions & 3 deletions test/unit/servicesSpec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
'use strict';

/* jasmine specs for services go here */

describe('service', function() {

});
// load modules
beforeEach(module('phonecatApp'));

// Test service availability
it('check the existence of Phone factory', inject(function(Phone) {
expect(Phone).toBeDefined();
}));
});

0 comments on commit 3a69cd6

Please sign in to comment.