Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 15c1fe3

Browse files
committed
refactor(ngView): remove extra $watch, refactor one ugly test
1 parent 428f2b5 commit 15c1fe3

File tree

2 files changed

+37
-39
lines changed

2 files changed

+37
-39
lines changed

src/ng/directive/ngView.js

+19-17
Original file line numberDiff line numberDiff line change
@@ -115,42 +115,44 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
115115
lastScope,
116116
onloadExp = attr.onload || '';
117117

118-
scope.$on('$afterRouteChange', function(event, next, previous) {
119-
changeCounter++;
120-
});
118+
scope.$on('$afterRouteChange', update);
119+
update();
121120

122-
scope.$watch(function() {return changeCounter;}, function(newChangeCounter) {
123-
var template = $route.current && $route.current.template;
124121

125-
function destroyLastScope() {
126-
if (lastScope) {
127-
lastScope.$destroy();
128-
lastScope = null;
129-
}
122+
function destroyLastScope() {
123+
if (lastScope) {
124+
lastScope.$destroy();
125+
lastScope = null;
130126
}
127+
}
128+
129+
function update() {
130+
var template = $route.current && $route.current.template,
131+
thisChangeId = ++changeCounter;
131132

132133
function clearContent() {
133134
// ignore callback if another route change occured since
134-
if (newChangeCounter == changeCounter) {
135+
if (thisChangeId === changeCounter) {
135136
element.html('');
137+
destroyLastScope();
136138
}
137-
destroyLastScope();
138139
}
139140

140141
if (template) {
141142
$http.get(template, {cache: $templateCache}).success(function(response) {
142143
// ignore callback if another route change occured since
143-
if (newChangeCounter == changeCounter) {
144+
if (thisChangeId === changeCounter) {
144145
element.html(response);
145146
destroyLastScope();
146147

147148
var link = $compile(element.contents()),
148-
current = $route.current;
149+
current = $route.current,
150+
controller;
149151

150152
lastScope = current.scope = scope.$new();
151153
if (current.controller) {
152-
element.contents().
153-
data('$ngControllerController', $controller(current.controller, {$scope: lastScope}));
154+
controller = $controller(current.controller, {$scope: lastScope});
155+
element.contents().data('$ngControllerController', controller);
154156
}
155157

156158
link(lastScope);
@@ -164,7 +166,7 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
164166
} else {
165167
clearContent();
166168
}
167-
});
169+
}
168170
}
169171
};
170172
}];

test/ng/directive/ngViewSpec.js

+18-22
Original file line numberDiff line numberDiff line change
@@ -137,33 +137,29 @@ describe('ng-view', function() {
137137
});
138138

139139

140-
it('should be possible to nest ng-view in ng-include', inject(function() {
141-
// TODO(vojta): refactor this test
142-
dealoc(element);
143-
var injector = angular.injector(['ng', 'ngMock', function($routeProvider) {
144-
$routeProvider.when('/foo', {controller: angular.noop, template: 'viewPartial.html'});
145-
}]);
146-
var myApp = injector.get('$rootScope');
147-
var $httpBackend = injector.get('$httpBackend');
148-
$httpBackend.expect('GET', 'includePartial.html').respond('view: <ng:view></ng:view>');
149-
injector.get('$location').path('/foo');
140+
it('should be possible to nest ng-view in ng-include', function() {
141+
142+
module(function($routeProvider) {
143+
$routeProvider.when('/foo', {template: 'viewPartial.html'});
144+
});
150145

151-
var $route = injector.get('$route');
146+
inject(function($httpBackend, $location, $route, $compile, $rootScope) {
147+
$httpBackend.whenGET('includePartial.html').respond('view: <ng:view></ng:view>');
148+
$httpBackend.whenGET('viewPartial.html').respond('content');
149+
$location.path('/foo');
152150

153-
element = injector.get('$compile')(
151+
var elm = $compile(
154152
'<div>' +
155153
'include: <ng:include src="\'includePartial.html\'"> </ng:include>' +
156-
'</div>')(myApp);
157-
myApp.$apply();
158-
159-
$httpBackend.expect('GET', 'viewPartial.html').respond('content');
160-
$httpBackend.flush();
154+
'</div>')($rootScope);
155+
$rootScope.$digest();
156+
$httpBackend.flush();
161157

162-
expect(element.text()).toEqual('include: view: content');
163-
expect($route.current.template).toEqual('viewPartial.html');
164-
dealoc(myApp);
165-
dealoc(element);
166-
}));
158+
expect(elm.text()).toEqual('include: view: content');
159+
expect($route.current.template).toEqual('viewPartial.html');
160+
dealoc(elm)
161+
});
162+
});
167163

168164

169165
it('should initialize view template after the view controller was initialized even when ' +

0 commit comments

Comments
 (0)