mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-20 12:21:52 +00:00
refactor(ngView): remove extra $watch, refactor one ugly test
This commit is contained in:
parent
428f2b5636
commit
15c1fe3929
2 changed files with 37 additions and 39 deletions
|
|
@ -115,42 +115,44 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
|
||||||
lastScope,
|
lastScope,
|
||||||
onloadExp = attr.onload || '';
|
onloadExp = attr.onload || '';
|
||||||
|
|
||||||
scope.$on('$afterRouteChange', function(event, next, previous) {
|
scope.$on('$afterRouteChange', update);
|
||||||
changeCounter++;
|
update();
|
||||||
});
|
|
||||||
|
|
||||||
scope.$watch(function() {return changeCounter;}, function(newChangeCounter) {
|
|
||||||
var template = $route.current && $route.current.template;
|
|
||||||
|
|
||||||
function destroyLastScope() {
|
function destroyLastScope() {
|
||||||
if (lastScope) {
|
if (lastScope) {
|
||||||
lastScope.$destroy();
|
lastScope.$destroy();
|
||||||
lastScope = null;
|
lastScope = null;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function update() {
|
||||||
|
var template = $route.current && $route.current.template,
|
||||||
|
thisChangeId = ++changeCounter;
|
||||||
|
|
||||||
function clearContent() {
|
function clearContent() {
|
||||||
// ignore callback if another route change occured since
|
// ignore callback if another route change occured since
|
||||||
if (newChangeCounter == changeCounter) {
|
if (thisChangeId === changeCounter) {
|
||||||
element.html('');
|
element.html('');
|
||||||
|
destroyLastScope();
|
||||||
}
|
}
|
||||||
destroyLastScope();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (template) {
|
if (template) {
|
||||||
$http.get(template, {cache: $templateCache}).success(function(response) {
|
$http.get(template, {cache: $templateCache}).success(function(response) {
|
||||||
// ignore callback if another route change occured since
|
// ignore callback if another route change occured since
|
||||||
if (newChangeCounter == changeCounter) {
|
if (thisChangeId === changeCounter) {
|
||||||
element.html(response);
|
element.html(response);
|
||||||
destroyLastScope();
|
destroyLastScope();
|
||||||
|
|
||||||
var link = $compile(element.contents()),
|
var link = $compile(element.contents()),
|
||||||
current = $route.current;
|
current = $route.current,
|
||||||
|
controller;
|
||||||
|
|
||||||
lastScope = current.scope = scope.$new();
|
lastScope = current.scope = scope.$new();
|
||||||
if (current.controller) {
|
if (current.controller) {
|
||||||
element.contents().
|
controller = $controller(current.controller, {$scope: lastScope});
|
||||||
data('$ngControllerController', $controller(current.controller, {$scope: lastScope}));
|
element.contents().data('$ngControllerController', controller);
|
||||||
}
|
}
|
||||||
|
|
||||||
link(lastScope);
|
link(lastScope);
|
||||||
|
|
@ -164,7 +166,7 @@ var ngViewDirective = ['$http', '$templateCache', '$route', '$anchorScroll', '$c
|
||||||
} else {
|
} else {
|
||||||
clearContent();
|
clearContent();
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}];
|
}];
|
||||||
|
|
|
||||||
|
|
@ -137,33 +137,29 @@ describe('ng-view', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should be possible to nest ng-view in ng-include', inject(function() {
|
it('should be possible to nest ng-view in ng-include', function() {
|
||||||
// TODO(vojta): refactor this test
|
|
||||||
dealoc(element);
|
|
||||||
var injector = angular.injector(['ng', 'ngMock', function($routeProvider) {
|
|
||||||
$routeProvider.when('/foo', {controller: angular.noop, template: 'viewPartial.html'});
|
|
||||||
}]);
|
|
||||||
var myApp = injector.get('$rootScope');
|
|
||||||
var $httpBackend = injector.get('$httpBackend');
|
|
||||||
$httpBackend.expect('GET', 'includePartial.html').respond('view: <ng:view></ng:view>');
|
|
||||||
injector.get('$location').path('/foo');
|
|
||||||
|
|
||||||
var $route = injector.get('$route');
|
module(function($routeProvider) {
|
||||||
|
$routeProvider.when('/foo', {template: 'viewPartial.html'});
|
||||||
|
});
|
||||||
|
|
||||||
element = injector.get('$compile')(
|
inject(function($httpBackend, $location, $route, $compile, $rootScope) {
|
||||||
|
$httpBackend.whenGET('includePartial.html').respond('view: <ng:view></ng:view>');
|
||||||
|
$httpBackend.whenGET('viewPartial.html').respond('content');
|
||||||
|
$location.path('/foo');
|
||||||
|
|
||||||
|
var elm = $compile(
|
||||||
'<div>' +
|
'<div>' +
|
||||||
'include: <ng:include src="\'includePartial.html\'"> </ng:include>' +
|
'include: <ng:include src="\'includePartial.html\'"> </ng:include>' +
|
||||||
'</div>')(myApp);
|
'</div>')($rootScope);
|
||||||
myApp.$apply();
|
$rootScope.$digest();
|
||||||
|
$httpBackend.flush();
|
||||||
|
|
||||||
$httpBackend.expect('GET', 'viewPartial.html').respond('content');
|
expect(elm.text()).toEqual('include: view: content');
|
||||||
$httpBackend.flush();
|
expect($route.current.template).toEqual('viewPartial.html');
|
||||||
|
dealoc(elm)
|
||||||
expect(element.text()).toEqual('include: view: content');
|
});
|
||||||
expect($route.current.template).toEqual('viewPartial.html');
|
});
|
||||||
dealoc(myApp);
|
|
||||||
dealoc(element);
|
|
||||||
}));
|
|
||||||
|
|
||||||
|
|
||||||
it('should initialize view template after the view controller was initialized even when ' +
|
it('should initialize view template after the view controller was initialized even when ' +
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue