##Replay cached data when network/service fails
####Each successful request is cached in localStorage with url as the key. When the network/service fails, a cached response will be served up
npm install --save angular-http-backup
bower install --save angular-http-backup
angular
.module( 'your.module', [ 'httpbackup' ] )
.config( $httpProvider, httpBackupCacheProvider ) {
$httpProvider.interceptors.push( 'httpBackupInterceptor' );
// by default all urls are cached,
// this allows regex to limit what gets cached
httpBackupCacheProvider.setCachingRules( [
new RegExp( "^api\/v1" ), //cache request urls starting with "api/v1"
new RegExp("^(?!.*[.]html$).*$") //ignore request urls ending with ".html"
] );
}
// inject httpBackupCache
function controller(httpBackupCache){
//reset all keys
httpBackupCache.clear();
// remove individual key
httpBackupCache.removeItem('GET:api/v1/auth/login');
httpBackupCache.removeItem('POST:api/v1/auth/status');
httpBackupCache.removeItem('PUT:api/v1/auth/ping');
}
// event $emitted on $rootScope when a response is cached
$rootScope.$on( 'HttpBackup_cached', function( event, data ) {
console.log(data.url); // request url
console.log(data.response); // cached response
}
// event $emitted on $rootScope when a cached response is used
$rootScope.$on( 'HttpBackup_activated', function( event, data ) {
console.log(data.url); // request url
console.log(data.response); // cached response
}