Skip to content

Commit 5a74331

Browse files
Merge pull request chieffancypants#96 from just-boris/master
Use cache logic similar to angular core
2 parents f667ce0 + 20e59f3 commit 5a74331

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/loading-bar.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,15 @@ angular.module('cfp.loadingBarInterceptor', ['cfp.loadingBar'])
6767
*/
6868
function isCached(config) {
6969
var cache;
70+
var defaultCache = $cacheFactory.get('$http');
7071
var defaults = $httpProvider.defaults;
7172

72-
if (config.method !== 'GET' || config.cache === false) {
73-
config.cached = false;
74-
return false;
75-
}
76-
77-
if (config.cache === true && defaults.cache === undefined) {
78-
cache = $cacheFactory.get('$http');
79-
} else if (defaults.cache !== undefined) {
80-
cache = defaults.cache;
81-
} else {
82-
cache = config.cache;
73+
// Choose the proper cache source. Borrowed from angular: $http service
74+
if ((config.cache || defaults.cache) && config.cache !== false &&
75+
(config.method === 'GET' || config.method === 'JSONP')) {
76+
cache = angular.isObject(config.cache) ? config.cache
77+
: angular.isObject(defaults.cache) ? defaults.cache
78+
: defaultCache;
8379
}
8480

8581
var cached = cache !== undefined ?

test/loading-bar-interceptor.coffee

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,30 @@ describe 'loadingBarInterceptor Service', ->
111111
$httpBackend.verifyNoOutstandingRequest()
112112
$timeout.flush() # loading bar is animated, so flush timeout
113113

114+
it 'should use default cache when $http.defaults.cache is true', inject (cfpLoadingBar, $cacheFactory) ->
115+
# $http.defaults.cache = $cacheFactory('loading-bar')
116+
$http.defaults.cache = true
117+
$httpBackend.expectGET(endpoint).respond response
118+
$http.get(endpoint).then (data) ->
119+
result = data
120+
121+
expect(cfpLoadingBar.status()).toBe 0
122+
$timeout.flush()
123+
$timeout.flush()
124+
$httpBackend.flush(1)
125+
expect(cfpLoadingBar.status()).toBe 1
126+
cfpLoadingBar.complete() # set as complete
127+
$timeout.flush()
128+
$animate.triggerCallbacks()
129+
130+
131+
$http.get(endpoint).then (data) ->
132+
result = data
133+
# no need to flush $httpBackend since the response is cached
134+
expect(cfpLoadingBar.status()).toBe 0
135+
$httpBackend.verifyNoOutstandingRequest()
136+
$timeout.flush() # loading bar is animated, so flush timeout
137+
114138
it 'should not cache when the request is a POST', inject (cfpLoadingBar) ->
115139
$httpBackend.expectPOST(endpoint).respond response
116140
$http.post(endpoint, {message: 'post'}).then (data) ->

0 commit comments

Comments
 (0)