-
Notifications
You must be signed in to change notification settings - Fork 0
/
ape.js
71 lines (57 loc) · 1.89 KB
/
ape.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
58
59
60
61
62
63
64
65
66
67
68
69
var Ape = angular.module('Ape', []);
var serialize = function(obj, prefix) {
var str = [], p;
for(p in obj) {
if (obj.hasOwnProperty(p)) {
var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
str.push((v !== null && typeof v === "object") ?
serialize(v, k) :
encodeURIComponent(k) + "=" + encodeURIComponent(v));
}
}
return str.join("&");
}
Ape.factory('Ape', function($http){
var _Ape = {start : function(){ }, base : "",headers: {}, end : function(response){ } };
//configs ,
var urlReq = function(verb,url, data,callback){
var lastError = {};
_Ape.start();
$http({
method: verb,
url: _Ape.base + url + ( ( verb != "POST" && verb != "PUT" && Object.keys(data).length > 0 ) ? "?" + serialize( data, "") : "" ),
data: data,
headers : _Ape.headers
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
// console.log(response);
_Ape.end(response.data,response);
callback(response.data, response);
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
// $(".loader-overlay").css('display', 'none');
_Ape.end(false, response);
callback(false, response);
});
};
return {
Init: function(Ape){
_Ape = Ape;
},
LoopEq: function(arr, key, value){
for (var i = arr.length - 1; i >= 0; i--) {
if( arr[i][key] == value){
return arr[i]
}
};
return false;
},
Request: function(method,url, data,callback){
urlReq(method,url,data,callback);
}, getSelf: function(){
return _Ape;
}
}
});