Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow requests to be manually cancelled on broadcast from outside of l… #338

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ The loading bar broadcasts the following events over $rootScope allowing further

**`cfpLoadingBar:completed`** triggered once when the all XHR requests have returned (either successfully or not)

The loading bar can also be manually closed by firing an event

**`cfpLoadingBar:manual-loaded`** Trigger this if you want to leave connection open, but cancel loading bar

## Credits:
Credit goes to [rstacruz](https://github.com/rstacruz) for his excellent [nProgress](https://github.com/rstacruz/nprogress).

Expand Down
13 changes: 13 additions & 0 deletions src/loading-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ angular.module('cfp.loadingBarInterceptor', ['cfp.loadingBar'])
return cached;
}

$rootScope.$on('cfpLoadingBar:manual-loaded', function(event, config) {

if (!config.result.config.ignoreLoadingBar && !isCached(config.result.config)) {
reqsCompleted++;
$rootScope.$broadcast('cfpLoadingBar:loaded',config);
if (reqsCompleted >= reqsTotal) {
setComplete();
} else {
cfpLoadingBar.set(reqsCompleted / reqsTotal);
}
}
});


return {
'request': function(config) {
Expand Down
18 changes: 18 additions & 0 deletions test/loading-bar-interceptor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,24 @@ describe 'loadingBarInterceptor Service', ->
expect(cfpLoadingBar.status()).toBe 1

$timeout.flush()

it 'should count http manual loaded event as responses so the loading bar can complete', inject (cfpLoadingBar, $rootScope) ->
# $httpBackend.expectGET(endpoint).respond response
$httpBackend.expectGET(endpoint).respond 401
$httpBackend.expectGET(endpoint).respond 401
$http.get(endpoint)
$http.get(endpoint)

expect(cfpLoadingBar.status()).toBe 0
$timeout.flush()
$timeout.flush()
$httpBackend.flush(1)
expect(cfpLoadingBar.status()).toBe 0.5

$rootScope.$emit('cfpLoadingBar:manual-loaded', {url: endpoint, result: { config: { cache : false}}})
expect(cfpLoadingBar.status()).toBe 1

$timeout.flush()

it 'should insert the loadingbar into the DOM when a request is sent', inject (cfpLoadingBar) ->
$httpBackend.expectGET(endpoint).respond response
Expand Down