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

Commit b2fc39d

Browse files
feat($templateRequest): support configuration of $http options
It is now possible to configure the options sent to $http for template requests. If no value is configured then the request will use the default $http options. Thanks to @luckycadow for help on this feature Closes #13188 Closes #11868 Closes #6860
1 parent 7c0731e commit b2fc39d

File tree

2 files changed

+131
-22
lines changed

2 files changed

+131
-22
lines changed

src/ng/templateRequest.js

+54-22
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,60 @@
33
var $compileMinErr = minErr('$compile');
44

55
/**
6-
* @ngdoc service
7-
* @name $templateRequest
8-
*
6+
* @ngdoc provider
7+
* @name $templateRequestProvider
98
* @description
10-
* The `$templateRequest` service runs security checks then downloads the provided template using
11-
* `$http` and, upon success, stores the contents inside of `$templateCache`. If the HTTP request
12-
* fails or the response data of the HTTP request is empty, a `$compile` error will be thrown (the
13-
* exception can be thwarted by setting the 2nd parameter of the function to true). Note that the
14-
* contents of `$templateCache` are trusted, so the call to `$sce.getTrustedUrl(tpl)` is omitted
15-
* when `tpl` is of type string and `$templateCache` has the matching entry.
16-
*
17-
* @param {string|TrustedResourceUrl} tpl The HTTP request template URL
18-
* @param {boolean=} ignoreRequestError Whether or not to ignore the exception when the request fails or the template is empty
19-
*
20-
* @return {Promise} a promise for the HTTP response data of the given URL.
21-
*
22-
* @property {number} totalPendingRequests total amount of pending template requests being downloaded.
9+
* Used to configure the Accept header that is sent to the server when requesting a template.
2310
*/
2411
function $TemplateRequestProvider() {
12+
13+
var httpOptions;
14+
15+
/**
16+
* @ngdoc method
17+
* @name $templateRequestProvider#httpOptions
18+
* @description
19+
* The options to be passed to the $http service when making the request.
20+
* You can use this to override options such as the Accept header for template requests.
21+
*
22+
* The {$templateRequest} will set the `cache` and the `transformResponse` properties of the
23+
* options if not overridden here.
24+
*
25+
* @param {string=} value new value for the {@link $http} options.
26+
* @returns {string|self} Returns the {@link $http} options when used as getter and self if used as setter.
27+
*/
28+
this.httpOptions = function(val) {
29+
if (val) {
30+
httpOptions = val;
31+
return this;
32+
}
33+
return httpOptions;
34+
};
35+
36+
/**
37+
* @ngdoc service
38+
* @name $templateRequest
39+
*
40+
* @description
41+
* The `$templateRequest` service runs security checks then downloads the provided template using
42+
* `$http` and, upon success, stores the contents inside of `$templateCache`. If the HTTP request
43+
* fails or the response data of the HTTP request is empty, a `$compile` error will be thrown (the
44+
* exception can be thwarted by setting the 2nd parameter of the function to true). Note that the
45+
* contents of `$templateCache` are trusted, so the call to `$sce.getTrustedUrl(tpl)` is omitted
46+
* when `tpl` is of type string and `$templateCache` has the matching entry.
47+
*
48+
* If you want to pass custom options to the `$http` service, such as setting the Accept header you
49+
* can configure this via {@link $templateRequestProvider#httpOptions}.
50+
*
51+
* @param {string|TrustedResourceUrl} tpl The HTTP request template URL
52+
* @param {boolean=} ignoreRequestError Whether or not to ignore the exception when the request fails or the template is empty
53+
*
54+
* @return {Promise} a promise for the HTTP response data of the given URL.
55+
*
56+
* @property {number} totalPendingRequests total amount of pending template requests being downloaded.
57+
*/
2558
this.$get = ['$templateCache', '$http', '$q', '$sce', function($templateCache, $http, $q, $sce) {
59+
2660
function handleRequestFn(tpl, ignoreRequestError) {
2761
handleRequestFn.totalPendingRequests++;
2862

@@ -45,12 +79,10 @@ function $TemplateRequestProvider() {
4579
transformResponse = null;
4680
}
4781

48-
var httpOptions = {
49-
cache: $templateCache,
50-
transformResponse: transformResponse
51-
};
52-
53-
return $http.get(tpl, httpOptions)
82+
return $http.get(tpl, extend({
83+
cache: $templateCache,
84+
transformResponse: transformResponse
85+
}, httpOptions))
5486
['finally'](function() {
5587
handleRequestFn.totalPendingRequests--;
5688
})

test/ng/templateRequestSpec.js

+77
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,83 @@
22

33
describe('$templateRequest', function() {
44

5+
describe('provider', function() {
6+
7+
describe('httpOptions', function() {
8+
9+
it('should default to undefined and fallback to default $http options', function() {
10+
11+
var defaultHeader;
12+
13+
module(function($templateRequestProvider) {
14+
expect($templateRequestProvider.httpOptions()).toBeUndefined();
15+
});
16+
17+
inject(function($templateRequest, $http, $templateCache) {
18+
spyOn($http, 'get').andCallThrough();
19+
20+
$templateRequest('tpl.html');
21+
22+
expect($http.get).toHaveBeenCalledOnceWith('tpl.html', {
23+
cache: $templateCache,
24+
transformResponse: [ ]
25+
});
26+
});
27+
28+
});
29+
30+
it('should be configurable', function() {
31+
32+
function someTransform() {}
33+
34+
module(function($templateRequestProvider, $httpProvider) {
35+
36+
// Configure the template request service to provide specific headers and transforms
37+
$templateRequestProvider.httpOptions({
38+
headers: { Accept: 'moo' },
39+
transformResponse: [someTransform]
40+
});
41+
});
42+
43+
inject(function($templateRequest, $http, $templateCache) {
44+
spyOn($http, 'get').andCallThrough();
45+
46+
$templateRequest('tpl.html');
47+
48+
expect($http.get).toHaveBeenCalledOnceWith('tpl.html', {
49+
cache: $templateCache,
50+
transformResponse: [someTransform],
51+
headers: { Accept: 'moo' }
52+
});
53+
});
54+
});
55+
56+
57+
it('should be allow you to override the cache', function() {
58+
59+
var httpOptions = {};
60+
61+
module(function($templateRequestProvider, $httpProvider) {
62+
$templateRequestProvider.httpOptions(httpOptions);
63+
});
64+
65+
inject(function($templateRequest, $http, $cacheFactory) {
66+
spyOn($http, 'get').andCallThrough();
67+
68+
var customCache = $cacheFactory('customCache');
69+
httpOptions.cache = customCache;
70+
71+
$templateRequest('tpl.html');
72+
73+
expect($http.get).toHaveBeenCalledOnceWith('tpl.html', {
74+
cache: customCache,
75+
transformResponse: []
76+
});
77+
});
78+
});
79+
});
80+
});
81+
582
it('should download the provided template file',
683
inject(function($rootScope, $templateRequest, $httpBackend) {
784

0 commit comments

Comments
 (0)