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

Commit fb00991

Browse files
frederikprijckpetebacondarwin
authored andcommitted
fix($templateRequest): always return the template that is stored in the cache
Previously, `$templateRequest` returned the raw `$http` response data on the first request for a template and then the value from the cache for subsequent requests. If the value is transformed when being added to the cache (by decorating `$templateCache.put`) the return value of `$templateRequest` would be inconsistent depending upon when the request is made. This commit ensures the cached value is returned instead of the raw `$http` response data, thus allowing the `$templateCache` service to be decorated. Closes #16225
1 parent d3bffc5 commit fb00991

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/ng/templateRequest.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ function $TemplateRequestProvider() {
9999
handleRequestFn.totalPendingRequests--;
100100
})
101101
.then(function(response) {
102-
$templateCache.put(tpl, response.data);
103-
return response.data;
102+
return $templateCache.put(tpl, response.data);
104103
}, handleError);
105104

106105
function handleError(resp) {

test/ng/templateRequestSpec.js

+18
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,24 @@ describe('$templateRequest', function() {
114114
expect($templateCache.get('tpl.html')).toBe('matias');
115115
}));
116116

117+
it('should return the cached value on the first request',
118+
inject(function($rootScope, $templateRequest, $templateCache, $httpBackend) {
119+
120+
$httpBackend.expectGET('tpl.html').respond('matias');
121+
spyOn($templateCache, 'put').and.returnValue('_matias');
122+
123+
var content = [];
124+
function tplRequestCb(html) {
125+
content.push(html);
126+
}
127+
128+
$templateRequest('tpl.html').then(tplRequestCb);
129+
$rootScope.$digest();
130+
$httpBackend.flush();
131+
132+
expect(content[0]).toBe('_matias');
133+
}));
134+
117135
it('should call `$exceptionHandler` on request error', function() {
118136
module(function($exceptionHandlerProvider) {
119137
$exceptionHandlerProvider.mode('log');

0 commit comments

Comments
 (0)