albertbrand / angular-cancel-on-navigate Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
This modified source code solves 2 problems.
I didn't create a PR because these changes are easy to implement. Source code below:
angular
.module('angularCancelOnNavigateModule', [])
.config(function($httpProvider) {
$httpProvider.interceptors.push('HttpRequestTimeoutInterceptor');
})
.run(function ($rootScope, HttpPendingRequestsService) {
$rootScope.$on('$locationChangeSuccess', function (event, newUrl, oldUrl) {
if (newUrl != oldUrl) {
HttpPendingRequestsService.cancelAll();
}
});
});
angular.module('angularCancelOnNavigateModule')
.service('HttpPendingRequestsService', function ($q) {
var cancelPromises = [];
function newTimeout() {
var cancelPromise = $q.defer();
cancelPromises.push(cancelPromise);
return cancelPromise.promise;
}
function cancelAll() {
angular.forEach(cancelPromises, function (cancelPromise) {
cancelPromise.promise.isGloballyCancelled = true;
cancelPromise.resolve();
});
cancelPromises.length = 0;
}
return {
newTimeout: newTimeout,
cancelAll: cancelAll
};
});
angular.module('angularCancelOnNavigateModule')
.factory('HttpRequestTimeoutInterceptor', function ($q, HttpPendingRequestsService) {
return {
request: function (config) {
config = config || {};
if (config.timeout === undefined && config.cancelOnRouteChange) {
config.timeout = HttpPendingRequestsService.newTimeout();
}
return config;
},
responseError: function (response) {
return $q.reject(response);
}
};
});
Hi,
Here's my objective, i want to cancel all HTTP request from my previous when im going to another module. Something like this dashboard > company.
I want to cancel all HTTP request form dashboard module when i successfully change module. If this possible please guide me.
Thanks in advance for your help.
Emman
The dependency injection breaks when you minify the file. I believe the minified file included in the build directory is also broken.
Hello,
note sure if this project is still active or not . In case it is, would it be possible to have an NPM release?
Hi, I was very frustrated at first because this library wasn't working OK, but after a while I realized that this module uses $locationChangeSuccess event.
Because I'm using ui-router with states, I simple had to replace that event name with '$stateChangeSuccess' and that solved my issue.
I think it could be helpful for others to make this observation in your README, cause ui-router is basically a standard on these days.
Thanks for the library anyway!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.