feat($templateRequest): introduce the $templateRequest service

This handy service is designed to download and cache template contents
and to throw an error when a template request fails.

BREAKING CHANGE

Angular will now throw a $compile minErr each a template fails to download
for ngView, directives and ngMessage template requests. This changes the former
behavior of silently ignoring failed HTTP requests--or when the template itself
is empty. Please ensure that all directive, ngView and ngMessage code now properly
addresses this scenario. NgInclude is uneffected from this change.
This commit is contained in:
Matias Niemelä
2014-08-12 12:40:17 -04:00
parent 3be00df495
commit a70e2833ea
11 changed files with 190 additions and 45 deletions

View File

@@ -199,6 +199,7 @@ describe('ngInclude', function() {
$rootScope.url = 'url2';
$rootScope.$digest();
$httpBackend.flush();
expect($rootScope.$$childHead).toBeFalsy();
expect(element.text()).toBe('');

View File

@@ -0,0 +1,89 @@
'use strict';
describe('$templateRequest', function() {
it('should download the provided template file',
inject(function($rootScope, $templateRequest, $httpBackend) {
$httpBackend.expectGET('tpl.html').respond('<div>abc</div>');
var content;
$templateRequest('tpl.html').then(function(html) { content = html; });
$rootScope.$digest();
$httpBackend.flush();
expect(content).toBe('<div>abc</div>');
}));
it('should cache the request using $templateCache to prevent extra downloads',
inject(function($rootScope, $templateRequest, $templateCache) {
$templateCache.put('tpl.html', 'matias');
var content;
$templateRequest('tpl.html').then(function(html) { content = html; });
$rootScope.$digest();
expect(content).toBe('matias');
}));
it('should throw an error when the template is not found',
inject(function($rootScope, $templateRequest, $httpBackend) {
$httpBackend.expectGET('tpl.html').respond(404);
$templateRequest('tpl.html');
$rootScope.$digest();
expect(function() {
$rootScope.$digest();
$httpBackend.flush();
}).toThrowMinErr('$compile', 'tpload', 'Failed to load template: tpl.html');
}));
it('should throw an error when the template is empty',
inject(function($rootScope, $templateRequest, $httpBackend) {
$httpBackend.expectGET('tpl.html').respond('');
$templateRequest('tpl.html');
$rootScope.$digest();
expect(function() {
$rootScope.$digest();
$httpBackend.flush();
}).toThrowMinErr('$compile', 'tpload', 'Failed to load template: tpl.html');
}));
it('should keep track of how many requests are going on',
inject(function($rootScope, $templateRequest, $httpBackend) {
$httpBackend.expectGET('a.html').respond('a');
$httpBackend.expectGET('b.html').respond('c');
$templateRequest('a.html');
$templateRequest('b.html');
expect($templateRequest.totalPendingRequests).toBe(2);
$rootScope.$digest();
$httpBackend.flush();
expect($templateRequest.totalPendingRequests).toBe(0);
$httpBackend.expectGET('c.html').respond(404);
$templateRequest('c.html');
expect($templateRequest.totalPendingRequests).toBe(1);
$rootScope.$digest();
try {
$httpBackend.flush();
} catch(e) {}
expect($templateRequest.totalPendingRequests).toBe(0);
}));
});

View File

@@ -56,7 +56,7 @@ describe('ngView', function() {
});
it('should instantiate controller for empty template', function() {
it('should not instantiate the associated controller when an empty template is downloaded', function() {
var log = [], controllerScope,
Ctrl = function($scope) {
controllerScope = $scope;
@@ -70,11 +70,12 @@ describe('ngView', function() {
inject(function($route, $rootScope, $templateCache, $location) {
$templateCache.put('/tpl.html', [200, '', {}]);
$location.path('/some');
$rootScope.$digest();
expect(controllerScope.$parent).toBe($rootScope);
expect(controllerScope).toBe($route.current.scope);
expect(log).toEqual(['ctrl-init']);
expect(function() {
$rootScope.$digest();
}).toThrowMinErr('$compile', 'tpload', 'Failed to load template: /tpl.html');
expect(controllerScope).toBeUndefined();
});
});

View File

@@ -671,35 +671,36 @@ describe('$route', function() {
});
it('should drop in progress route change when new route change occurs and old fails', function() {
module(function($routeProvider) {
it('should throw an error when a template is empty or not found', function() {
module(function($routeProvider, $exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
$routeProvider.
when('/r1', { templateUrl: 'r1.html' }).
when('/r2', { templateUrl: 'r2.html' });
when('/r2', { templateUrl: 'r2.html' }).
when('/r3', { templateUrl: 'r3.html' });
});
inject(function($route, $httpBackend, $location, $rootScope) {
var log = '';
$rootScope.$on('$routeChangeError', function(e, next, last, error) {
log += '$failed(' + next.templateUrl + ', ' + error.status + ');';
});
$rootScope.$on('$routeChangeStart', function(e, next) { log += '$before(' + next.templateUrl + ');'; });
$rootScope.$on('$routeChangeSuccess', function(e, next) { log += '$after(' + next.templateUrl + ');'; });
inject(function($route, $httpBackend, $location, $rootScope, $exceptionHandler) {
$httpBackend.expectGET('r1.html').respond(404, 'R1');
$httpBackend.expectGET('r2.html').respond('R2');
$location.path('/r1');
$rootScope.$digest();
expect(log).toBe('$before(r1.html);');
$location.path('/r2');
$rootScope.$digest();
expect(log).toBe('$before(r1.html);$before(r2.html);');
$httpBackend.flush();
expect(log).toBe('$before(r1.html);$before(r2.html);$after(r2.html);');
expect(log).not.toContain('$after(r1.html);');
expect($exceptionHandler.errors.pop().message).toContain("[$compile:tpload] Failed to load template: r1.html");
$httpBackend.expectGET('r2.html').respond('');
$location.path('/r2');
$rootScope.$digest();
$httpBackend.flush();
expect($exceptionHandler.errors.pop().message).toContain("[$compile:tpload] Failed to load template: r2.html");
$httpBackend.expectGET('r3.html').respond('abc');
$location.path('/r3');
$rootScope.$digest();
$httpBackend.flush();
expect($exceptionHandler.errors.length).toBe(0);
});
});