-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathangular-xml.js
57 lines (50 loc) · 1.36 KB
/
angular-xml.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
53
54
55
56
57
if (!X2JS) {
throw new Error('You\'re required to include the X2JS library to use the xml module.');
}
(function(ng, X2JS) {
'use strict';
function responseIsXml(response) {
var contentType = response.headers('content-type');
if (contentType) {
return contentType.search(/\Wxml/i) > -1;
} else {
return false;
}
}
function xmlHttpInterceptorFactory($q, x2js) {
function responseHandler(response) {
if (response && responseIsXml(response)) {
response.data = x2js.xml_str2json(response.data);
return response;
} else {
return $q.when(response);
}
}
function responseErrorHandler(response) {
if (response && responseIsXml(response)) {
response.data = x2js.xml_str2json(response.data);
}
return $q.reject(response);
}
return {
response: responseHandler,
responseError: responseErrorHandler
};
}
function configProvider($provide) {
$provide.factory('xmlHttpInterceptor', ['$q', 'x2js', xmlHttpInterceptorFactory]);
}
function X2JSProvider() {
this.config = {};
this.$get = ['X2JS', function (X2JS) {
return new X2JS(this.config);
}];
}
if (ng) {
ng
.module('xml', [])
.config(['$provide', configProvider])
.provider('x2js', X2JSProvider)
.value('X2JS', X2JS);
}
}(angular, X2JS));