-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-http-alert.js
52 lines (38 loc) · 1.64 KB
/
angular-http-alert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(function() {
angular
.module('http-alert', ['angular-growl'])
.provider('httpAlertInterceptor', [httpAlertInterceptor])
function httpAlertInterceptor() {
var growlObj;
var alerter = {};
var filter = function(response) {return true;};
var responseParser = function(response) {
return 'An error ocurred. Http Response Body: ' + JSON.stringify(response.data);
};
var useTranslate = false;
alerter.error = function(message) {growlObj.error(message);};
this.getAlerter = function() {return alerter;};
this.setAlerter = function(newAlerter) {alerter = newAlerter;};
this.getFilter = function() {return filter;};
this.setFilter = function(newFilter) {filter = newFilter;};
this.getResponseParser = function() {return responseParser;};
this.setResponseParser = function(newResponseParser) {responseParser = newResponseParser;};
this.setUseTranslate = function(newUseTranslate) { useTranslate = newUseTranslate; };
this.$get = ['growl', '$filter', '$q', function(growl, $filter, $q) {
growlObj = growl;
var service = {};
service.responseError = function(config) {
if(!filter(config)) {
return $q.reject(config);
}
var message = responseParser(config);
if (useTranslate) {
message = $filter('translate')(message);
}
alerter.error(message);
return $q.reject(config);
}
return service;
}];
}
})();