Skip to content

Commit

Permalink
Merge pull request #96 from just-boris/master
Browse files Browse the repository at this point in the history
Use cache logic similar to angular core
  • Loading branch information
chieffancypants committed Aug 26, 2014
2 parents f667ce0 + 20e59f3 commit 5a74331
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/loading-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,15 @@ angular.module('cfp.loadingBarInterceptor', ['cfp.loadingBar'])
*/
function isCached(config) {
var cache;
var defaultCache = $cacheFactory.get('$http');
var defaults = $httpProvider.defaults;

if (config.method !== 'GET' || config.cache === false) {
config.cached = false;
return false;
}

if (config.cache === true && defaults.cache === undefined) {
cache = $cacheFactory.get('$http');
} else if (defaults.cache !== undefined) {
cache = defaults.cache;
} else {
cache = config.cache;
// Choose the proper cache source. Borrowed from angular: $http service
if ((config.cache || defaults.cache) && config.cache !== false &&
(config.method === 'GET' || config.method === 'JSONP')) {
cache = angular.isObject(config.cache) ? config.cache
: angular.isObject(defaults.cache) ? defaults.cache
: defaultCache;
}

var cached = cache !== undefined ?
Expand Down
24 changes: 24 additions & 0 deletions test/loading-bar-interceptor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,30 @@ describe 'loadingBarInterceptor Service', ->
$httpBackend.verifyNoOutstandingRequest()
$timeout.flush() # loading bar is animated, so flush timeout

it 'should use default cache when $http.defaults.cache is true', inject (cfpLoadingBar, $cacheFactory) ->
# $http.defaults.cache = $cacheFactory('loading-bar')
$http.defaults.cache = true
$httpBackend.expectGET(endpoint).respond response
$http.get(endpoint).then (data) ->
result = data

expect(cfpLoadingBar.status()).toBe 0
$timeout.flush()
$timeout.flush()
$httpBackend.flush(1)
expect(cfpLoadingBar.status()).toBe 1
cfpLoadingBar.complete() # set as complete
$timeout.flush()
$animate.triggerCallbacks()


$http.get(endpoint).then (data) ->
result = data
# no need to flush $httpBackend since the response is cached
expect(cfpLoadingBar.status()).toBe 0
$httpBackend.verifyNoOutstandingRequest()
$timeout.flush() # loading bar is animated, so flush timeout

it 'should not cache when the request is a POST', inject (cfpLoadingBar) ->
$httpBackend.expectPOST(endpoint).respond response
$http.post(endpoint, {message: 'post'}).then (data) ->
Expand Down

0 comments on commit 5a74331

Please sign in to comment.