Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix($templateRequest): always return the template that is stored in t…
…he 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
  • Loading branch information
frederikprijck committed Feb 4, 2018
commit a51c990af48f21ccf351cf96e8f7763fa419247a
3 changes: 1 addition & 2 deletions src/ng/templateRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ function $TemplateRequestProvider() {
handleRequestFn.totalPendingRequests--;
})
.then(function(response) {
$templateCache.put(tpl, response.data);
return response.data;
return $templateCache.put(tpl, response.data);
}, handleError);

function handleError(resp) {
Expand Down
18 changes: 18 additions & 0 deletions test/ng/templateRequestSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ describe('$templateRequest', function() {
expect($templateCache.get('tpl.html')).toBe('matias');
}));

it('should return the cached value on the first request',
inject(function($rootScope, $templateRequest, $templateCache, $httpBackend) {

$httpBackend.expectGET('tpl.html').respond('matias');
spyOn($templateCache, 'put').and.returnValue('_matias');

var content = [];
function tplRequestCb(html) {
content.push(html);
}

$templateRequest('tpl.html').then(tplRequestCb);
$rootScope.$digest();
$httpBackend.flush();

expect(content[0]).toBe('_matias');
}));

it('should call `$exceptionHandler` on request error', function() {
module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
Expand Down