Skip to content

Instantly share code, notes, and snippets.

@kennethlynne
Created March 4, 2014 09:30
Show Gist options
  • Save kennethlynne/9343149 to your computer and use it in GitHub Desktop.
Save kennethlynne/9343149 to your computer and use it in GitHub Desktop.
mock-backend fake delay
angular.module('yourApp')
.config(function ($httpProvider, Config) {
if(!Config.useMocks) return;
$httpProvider.interceptors.push(function ($q, $timeout, Config, $log) {
return {
'request': function (config) {
$log.log('Requesting ' + config.url, config);
return config;
},
'response': function (response) {
var deferred = $q.defer();
if(response.config.url.indexOf(Config.view_dir) == 0) return response; //Let through views immideately
//Fake delay on response from APIs and other urls
$log.log('Delaying response with ' + Config.API.fakeDelay + 'ms');
$timeout(function () {
deferred.resolve(response);
}, Config.API.fakeDelay);
return deferred.promise;
}
}
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment