Skip to content

Instantly share code, notes, and snippets.

@aBuder
Last active August 29, 2015 14:16
Show Gist options
  • Save aBuder/63cc09eb62e9e007b5ec to your computer and use it in GitHub Desktop.
Save aBuder/63cc09eb62e9e007b5ec to your computer and use it in GitHub Desktop.
REST-Angular Best Configuration
RestangularProvider.setBaseUrl("/web");
RestangularProvider.setListTypeIsArray(false);
RestangularProvider.setResponseExtractor(function(response, operation, what, url) {
var rootScope = angular.element(document).injector().get('$rootScope');
rootScope.$emit('UNLOAD');
if (operation === "getList") {
console.log('RestAngular: getList');
if (angular.isObject(response.content) && angular.isArray(response.content)) {
angular.forEach(response.content, function(content, key) {
entityId(content);
});
}
} else if (operation == "get") {
if (angular.isObject(response))
entityId(response);
} else if (operation == "post") {
if (angular.isObject(response))
entityId(response);
}
return response;
});
RestangularProvider.setRequestInterceptor(function(request, operation, route) {
var rootScope = angular.element(document).injector().get('$rootScope');
rootScope.$emit('LOAD');
if (operation === 'put') {
if (request.links)
delete request.links;
}
return request;
});
RestangularProvider.setErrorInterceptor(function (response) {
var rootScope = angular.element(document).injector().get('$rootScope');
rootScope.$emit('UNLOAD');
if(response.status == 401){
window.location.href = '/web';
return false;
}
return true;
});
$httpProvider.defaults.headers.common['Content-Type'] = 'application/json;charset=UTF-8';
$httpProvider.defaults.headers.common['Accept'] = 'application/json;charset=UTF-8';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment