forked from trycom/parse-angular-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-angular.js
More file actions
105 lines (85 loc) · 2.97 KB
/
Copy pathparse-angular.js
File metadata and controls
105 lines (85 loc) · 2.97 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
(function(window, undef){
var angular = window.angular;
if (angular !== undef) {
var module = angular.module('parse-angular', []);
module.run(['$q', '$window', function($q, $window){
// Process only if Parse exist on the global window, do nothing otherwise
if (!angular.isUndefined($window.Parse) && angular.isObject($window.Parse)) {
// Keep a handy local reference
var Parse = $window.Parse;
//-------------------------------------
// Structured object of what we need to update
//-------------------------------------
var methodsToUpdate = {
"Object": {
prototype: ['save', 'fetch', 'destroy'],
static: ['saveAll', 'destroyAll']
},
"File": {
prototype: ['save'],
static: []
},
"Query": {
prototype: ['find', 'first', 'count', 'get'],
static: []
},
"Cloud": {
prototype: [],
static: ['run']
},
"User": {
prototype: ['signUp'],
static: ['requestPasswordReset', 'logIn']
},
"FacebookUtils": {
prototype: [],
static: ['logIn', 'link', 'unlink']
},
"Config": {
prototype: [],
static: ['get']
}
};
//// Let's loop over Parse objects
for (var k in methodsToUpdate) {
var currentClass = k;
var currentObject = methodsToUpdate[k];
var currentProtoMethods = currentObject.prototype;
var currentStaticMethods = currentObject.static;
/// Patching prototypes
currentProtoMethods.forEach(function(method){
var origMethod = Parse[currentClass].prototype[method];
// Overwrite original function by wrapping it with $q
Parse[currentClass].prototype[method] = function() {
return origMethod.apply(this, arguments).then(function(data){
var defer = $q.defer();
defer.resolve(data);
return defer.promise;
}, function(err){
var defer = $q.defer();
defer.reject(err);
return defer.promise;
});
};
});
///Patching static methods too
currentStaticMethods.forEach(function(method){
var origMethod = Parse[currentClass][method];
// Overwrite original function by wrapping it with $q
Parse[currentClass][method] = function() {
return origMethod.apply(this, arguments).then(function(data){
var defer = $q.defer();
defer.resolve(data);
return defer.promise;
}, function(err){
var defer = $q.defer();
defer.reject(err);
return defer.promise;
});
};
});
}
}
}]);
}
})(this);