Skip to content

Commit b490bc3

Browse files
IgorMinartbosch
authored andcommitted
step-5 XHR and dependency injection
- Replaced the in-memory dataset with data loaded from the server (in the form of static phone.json file to make this tutorial backend agnostic) - The json file is loaded using the [$http] service - Demonstrate the use of [services][service] and [dependency injection][DI] - The [$http] is injected into the controller through [dependency injection][DI]
1 parent 65559ae commit b490bc3

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

app/js/controllers.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,10 @@
44

55
var phonecatApp = angular.module('phonecatApp', []);
66

7-
phonecatApp.controller('PhoneListCtrl', function($scope) {
8-
$scope.phones = [
9-
{'name': 'Nexus S',
10-
'snippet': 'Fast just got faster with Nexus S.',
11-
'age': 1},
12-
{'name': 'Motorola XOOM™ with Wi-Fi',
13-
'snippet': 'The Next, Next Generation tablet.',
14-
'age': 2},
15-
{'name': 'MOTOROLA XOOM™',
16-
'snippet': 'The Next, Next Generation tablet.',
17-
'age': 3}
18-
];
7+
phonecatApp.controller('PhoneListCtrl', function($scope, $http) {
8+
$http.get('phones/phones.json').success(function(data) {
9+
$scope.phones = data;
10+
});
1911

2012
$scope.orderProp = 'age';
2113
});

test/e2e/scenarios.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ describe('PhoneCat App', function() {
1212

1313

1414
it('should filter the phone list as user types into the search box', function() {
15-
expect(repeater('.phones li').count()).toBe(3);
15+
expect(repeater('.phones li').count()).toBe(20);
1616

1717
input('query').enter('nexus');
1818
expect(repeater('.phones li').count()).toBe(1);
1919

2020
input('query').enter('motorola');
21-
expect(repeater('.phones li').count()).toBe(2);
21+
expect(repeater('.phones li').count()).toBe(8);
2222
});
2323

2424

test/unit/controllersSpec.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,28 @@
44
describe('PhoneCat controllers', function() {
55

66
describe('PhoneListCtrl', function(){
7-
var scope, ctrl;
7+
var scope, ctrl, $httpBackend;
88

99
beforeEach(module('phonecatApp'));
10+
beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
11+
$httpBackend = _$httpBackend_;
12+
$httpBackend.expectGET('phones/phones.json').
13+
respond([{name: 'Nexus S'}, {name: 'Motorola DROID'}]);
1014

11-
beforeEach(inject(function($controller) {
12-
scope = {};
13-
ctrl = $controller('PhoneListCtrl', {$scope:scope});
15+
scope = $rootScope.$new();
16+
ctrl = $controller('PhoneListCtrl', {$scope: scope});
1417
}));
1518

1619

20+
it('should create "phones" model with 2 phones fetched from xhr', function() {
21+
expect(scope.phones).toBeUndefined();
22+
$httpBackend.flush();
23+
24+
expect(scope.phones).toEqual([{name: 'Nexus S'},
25+
{name: 'Motorola DROID'}]);
26+
});
27+
28+
1729
it('should set the default value of orderProp model', function() {
1830
expect(scope.orderProp).toBe('age');
1931
});

0 commit comments

Comments
 (0)