Repository navigation
Expand file tree
/
Copy pathservices.http.js
More file actions
164 lines (145 loc) · 4.75 KB
/
Copy pathservices.http.js
File metadata and controls
164 lines (145 loc) · 4.75 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
(function() {
'use strict';
function SampleHttpService($q, $http, $filter) {
// AngularJS will instantiate a singleton by calling "new" on this function
var _params = {
url: '',
type : '_ngCache'+Math.floor((Math.random()*1000000)+1),
cache: true
};
//Making possible to instance the typeof ModelCore
return {
/**
* Params passed for HTTP request
* @type {[type]}
*/
_params: _params,
/**
* Response Cached
* @type {Object}
*/
_cache: false,
init: function(params){
var self = this;
// Extend params for create a factory in service
angular.extend(self._params, params );
return self;
},
/**
* Get value of sessionStorage cache information
* @return {[type]} [description]
*/
_getCacheInfo: function(){
if (this._cache !== false) {
return this._cache;
} else {
var item = window.sessionStorage.getItem(this._params.type);
return (item !== null) ? item : false;
}
},
/**
* Set value for sessionStorage cache information
* @param {[type]} data [description]
*/
_setCacheInfo: function(data){
this._cache = data;
window.sessionStorage.setItem(this._params.type, data);
},
/**
* Return a promise with values of $http response
* the $http API is based on the deferred/promise APIs exposed by the $q service
* so it returns a promise for us by default
* @return {[type]} [description]
*/
$get: function(url){
var deferred = $q.defer();
if ( !!this._params.cache && this._getCacheInfo() !== false ) {
deferred.resolve(this._getCacheInfo());
} else {
var self = this;
$http.get( this._params.url + url ).success(function(data){
self._setCacheInfo(data);
deferred.resolve(data);
}).error(function(error){
deferred.reject(error);
});
}
return deferred.promise;
},
/**
* Find one element based in params passed for filter
* @param {[type]} params parameters object for search in dataset
* @return {[type]} [description]
*/
$find: function(url, params){
var deferred = $q.defer();
this.$get(url).then(function(data){
var result = $filter('filter')(data, params);
deferred.resolve(result);
});
return deferred.promise;
},
/**
* Return a promise with values of $http response using HTTP POST verb
*
* Used (normally) when you want insert a item in API
*
* the $http API is based on the deferred/promise APIs exposed by the $q service
* so it returns a promise for us by default
* @return {[type]} [description]
*/
$post: function(url, params, headers){
params = params || {};
headers = headers || {};
var deferred = $q.defer();
$http.post( this._params.url + url, params, headers ).success(function(data){
deferred.resolve(data);
}).error(function(error){
deferred.reject(error);
});
return deferred.promise;
},
/**
* Return a promise with values of $http response using HTTP PUT verb
*
* Used when you want update a item in API
*
* the $http API is based on the deferred/promise APIs exposed by the $q service
* so it returns a promise for us by default
* @return {[type]} [description]
*/
$put: function(url, params, headers){
params = params || {};
headers = headers || {};
var deferred = $q.defer();
$http.put( this._params.url + url, params, headers ).success(function(data){
deferred.resolve(data);
}).error(function(error){
deferred.reject(error);
});
return deferred.promise;
},
/**
* Return a promise with values of $http response using HTTP DELETE verb
*
* Used when you want delete a item in API
*
* the $http API is based on the deferred/promise APIs exposed by the $q service
* so it returns a promise for us by default
* @return {[type]} [description]
*/
$delete: function(url){
var deferred = $q.defer();
$http.delete( this._params.url + url).success(function(data){
deferred.resolve(data);
}).error(function(error){
deferred.reject(error);
});
return deferred.promise;
},
};
}
angular.module('myApp')
.factory('SampleHttpService', SampleHttpService);
SampleHttpService.$inject = ['$q', '$http', '$filter'];
}());