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
224 lines (188 loc) · 7.64 KB
/
Copy pathparse-angular.js
File metadata and controls
224 lines (188 loc) · 7.64 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
(function(window, undef){
'use strict';
var angular = window.angular;
if (angular !== undef) {
angular
.module('parse-angular', [])
.run(['$q', '$window', function($q, $window) {
var Parse = $window.Parse;
if (!angular.isUndefined(Parse) && angular.isObject(Parse)) {
var methodsToUpdatePerParseObject = {
'Object': {
prototype: ['save', 'fetch', 'destroy'],
static: ['saveAll', 'destroyAll']
},
'Collection': {
prototype: ['fetch'],
static: []
},
'Query': {
prototype: ['find', 'first', 'count', 'get'],
static: []
},
'Cloud': {
prototype: [],
static: ['run']
},
'User': {
prototype: ['signUp'],
static: ['requestPasswordReset', 'logIn']
},
'FacebookUtils': {
prototype: [],
static: ['logIn', 'link', 'unlink']
}
};
Parse._.each(methodsToUpdatePerParseObject, function(currentObject, currentClass) {
var prototypeMethods = {
methods: currentObject.prototype,
target: Parse[currentClass].prototype
};
var staticMethods = {
methods: currentObject.static,
target: Parse[currentClass]
};
Parse._.each([prototypeMethods, staticMethods], function(methodType) {
methodType.methods.forEach(function(method) {
var origMethod = methodType.target[method];
// Overwrite original function by wrapping it with $q
methodType.target[method] = function() {
return origMethod.apply(this, arguments)
.then(function(data){
return $q.when(data);
}, function(err){
return $q.reject(err);
});
};
});
});
});
}
}]);
angular
.module('parse-angular.enhance', ['parse-angular'])
.run(['$q', '$window', function($q, $window) {
var Parse = $window.Parse;
if (!angular.isUndefined(Parse) && angular.isObject(Parse)) {
/**
* Parse.Object
*/
/// Create a method to easily access our object
/// Because Parse.Object("xxxx") is actually creating an object and we can't access static methods
Parse.Object.getClass = function(className) {
return Parse.Object._classMap[className];
};
///// Override orig extend
var origObjectExtend = Parse.Object.extend;
Parse.Object.extend = function(protoProps) {
var newClass = origObjectExtend.apply(this, arguments);
if (Parse._.isObject(protoProps) && Parse._.isArray(protoProps.attrs)) {
var attrs = protoProps.attrs;
var methods = protoProps.methods;
/// Generate setters & getters
Parse._.each(attrs, function(currentAttr){
(function() {
var propName = currentAttr;
Object.defineProperty(newClass.prototype, propName, {
get : function(){ return this.get(propName); },
set : function(value){ this.set(propName, value); }
});
})();
});
/// Get angular bindable object
newClass.prototype.getBindableParseObject = function() {
return new BindableParseObject(this, attrs, methods);
};
/// Deep clone method to also clone relations
newClass.prototype.deepClone = function(relations) {
relations = relations || protoProps.relations;
var self = this;
var clone = self.clone();
if (Parse._.isObject(relations)) {
Parse._.each(relations, function(type, property) {
var value = clone.get(property);
if (Parse._.isArray(value)) {
var clones = [];
Parse._.each(value, function(item) {
if (Parse._.isObject(item) && Parse._.isFunction(item.deepClone)) {
clones.push(item.deepClone());
}
});
clone.set(property, clones);
} else if (Parse._.isObject(value) && Parse._.isFunction(value.deepClone)) {
clone.set(property, value.deepClone());
}
});
}
return clone;
};
/// Add shortcut to create Parse.Query
newClass.query = function() {
return new Parse.Query(newClass);
};
}
return newClass;
};
/**
* Parse.Collection
*/
/// Keep references & init collection class map
Parse.Collection._classMap = {};
var origExtend = Parse.Collection.extend;
/// Enhance Collection 'extend' to store their subclass in a map
Parse.Collection.extend = function(opts) {
var extended = origExtend.apply(this, arguments);
if (opts && opts.className) {
Parse.Collection._classMap[opts.className] = extended;
}
return extended;
};
Parse.Collection.getClass = function(className) {
return Parse.Collection._classMap[className];
};
/// Enhance Collection prototype
Parse.Collection.prototype = angular.extend(Parse.Collection.prototype, {
// Simple paginator
loadMore: function(opts) {
if (!angular.isUndefined(this.query)) {
// Default Parse limit is 100
var currentLimit = this.query._limit === -1 ? 100 : this.query._limit;
var currentSkip = this.query._skip;
currentSkip += currentLimit;
this.query.skip(currentSkip);
var self = this;
return this.query.find().then(function(newModels) {
if (!opts || opts.add !== false) { self.add(newModels); }
if (newModels.length < currentLimit) { self.hasMoreToLoad = false; }
return newModels;
});
}
}
});
}
}]);
}
function BindableParseObject(parseObject, attrs, methods) {
var self = this,
_parseObject = parseObject;
self.getParseObject = function() {
return _parseObject;
};
methods = methods || [];
Parse._.each(['save', 'fetch', 'destroy', 'clone', 'deepClone'].concat(methods), function(method) {
self[method] = function() {
return _parseObject[method].apply(_parseObject, arguments);
};
});
Object.defineProperty(self, 'id', {
get : function(){ return _parseObject.id; }
});
Parse._.each(attrs, function(currentAttr) {
var propName = currentAttr;
Object.defineProperty(self, propName, {
get : function(){ return _parseObject.get(propName); },
set : function(value){ _parseObject.set(propName, value); }
});
});
}
})(this);