-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfirebase-resource.js
More file actions
404 lines (345 loc) · 11.8 KB
/
firebase-resource.js
File metadata and controls
404 lines (345 loc) · 11.8 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
angular.module('firebaseResource', []).
factory('firebaseResource', function($injector, $rootScope, $log, $timeout, $filter, $q, firebase) {
$rootScope.safeApply = function(fn) {
var phase = this.$root.$$phase;
if(phase == '$apply' || phase == '$digest') {
if(fn && (typeof(fn) === 'function')) {
fn();
}
} else {
this.$apply(fn);
}
};
function firebaseResourceFactory(opts) {
var options = opts ? opts : {};
var map = {};
var list = [];
var listenerPaths = {};
options.perPage = opts.perPage ? opts.perPage : 10;
var globalLimit = opts.limit ? opts.limit : 1000;
if (opts.path) {
var resourceRef = firebase.child(opts.path);
var resourcePath = firebase.path;
// var resourceQuery = firebase.child(opts.path).limit(globalLimit);
if (!opts.belongsTo) {
setListeners(opts.path, options);
}
}
function Resource(data) {
angular.copy(data || {}, this);
var _this = this;
if (this.id) {
setAssociations(this)
};
}
/***** Private Methods *****/
function setAssociations(self) {
var _this = self;
angular.forEach(options.hasMany, function(model) {
model = $injector.get(model);
var parent_path = Resource.getPath() + "/" + _this.id;
var parent_rels_path = Resource.getPath() + "/" + _this.id + "/rels/" + model.getName() + "/";
if (parent_path && !_this[model.getName()]) {
_this["_" + model.getName()] = [];
_this[model.getName()] = function() {
return {
query: function(opts, callback) {
var opts = opts ? opts : {};
opts.path = parent_rels_path;
opts.parent = _this;
return model.query(opts, callback);
},
all: function(opts) {
var opts = opts ? opts : {};
opts.parent = _this;
return model.all(opts);
},
new: function(data) {
var data = data ? data : {}
data["_" + Resource.getName() + "_parent_id"] = _this.id;
data._parent_path = parent_path;
data._parent_rels_path = parent_rels_path;
return new model(data);
},
add: function(obj) {
var deferred = $q.defer();
if (!obj._parent_rels_path) {
firebase.child(parent_rels_path + obj.id).once('value', function(s) {
if (s.val()) {
deferred.resolve(_this);
} else {
firebase.child(parent_rels_path).once('value', function(parentRels) {
var priority = parentRels.val() ? Object.keys(parentRels.val()).length : 1;
firebase.child(parent_rels_path + obj.id).setWithPriority(true, priority, function(error) {
if (error) {
$log.info('something went wrong: ' + error);
} else {
deferred.resolve(_this);
$rootScope.safeApply();
}
});
});
}
})
}
return deferred.promise;
}
};
};
};
});
}
function ensureRels(obj, relName) {
if (!obj.rels) {
obj.rels = {};
}
if (!obj.rels[relName]) {
obj.rels[relName] = {};
}
}
function getPagingQuery(parent, path, page) {
var perPage = options.perPage;
if (parent && page) {
ensureRels(parent, Resource.getName());
var total = Object.keys(parent.rels[Resource.getName()]).length;
var end = total-perPage*(page-1);
var start = total-perPage*page;
$log.info("start: " + start + ", end: " + end);
start = start < 1 ? 1 : start; // start cannot be less than 1;
end = end < start ? start : end; // end cannot be less than start;
if (page == 1) {
var query = firebase.child(path).startAt(start);
$log.info('no end');
} else {
var query = firebase.child(path).endAt(end).startAt(start);
}
path += "?page=" + page;
$log.info("start: " + start + ", end: " + end + ", path:" +path);
return query;
} else {
return false;
}
}
function setListeners(path, opts, callback) {
var opts = opts ? opts : {};
var query = getPagingQuery(opts.parent, path, opts.page);
if (!query) { query = firebase.child(path); };
if (opts.page) { path += "?page=" + opts.page; };
if (!listenerPaths[path]) {
listenerPaths[path] = true;
query.on('child_added', function(snapshot) {
$log.info('child_added');
if (opts.parent) {
firebase.child(Resource.getPath() + "/" + snapshot.name()).once('value', function(snap) {
if (snap.val()) {
var resource = updateResource(snap);
resource.init();
// add local variable for parent for filtering purposes.
resource["_" + opts.parent.getName() + "_parent_id"] = opts.parent.id;
resource._parent_path = opts.parent.getName() + "/" + opts.parent.id;
resource._parent_rels_path = opts.parent.getName() + "/" + opts.parent.id + "/rels/" + Resource.getName() + "/";
refreshRels(opts.parent);
if (callback) {
callback(resource);
}
}
})
} else {
var resource = updateResource(snapshot);
resource.init();
$rootScope.safeApply();
}
});
resourceRef.on('child_removed', function(snapshot) {
$log.info('child_removed');
removeResource(snapshot);
if (opts.parent) {
refreshRels(opts.parent);
}
$rootScope.safeApply();
});
resourceRef.on('child_changed', function(snapshot) {
$log.info('child_changed');
updateResource(snapshot);
$rootScope.safeApply();
});
}
}
function removeResource(snapshot) {
var name = snapshot.name ? snapshot.name() : snapshot.id;
if (map[name]) {
var index = list.indexOf(map[name]);
list.splice(index, 1);
delete map[name];
}
}
function updateResource(snapshot) {
var name = snapshot.name();
var data = snapshot.val();
if (data) {
if (map[name]) {
angular.forEach(data, function(val, key) {
map[name][key] = val;
});
var resource = map[name];
} else {
data.id = name;
var resource = new Resource(data);
addResource(resource);
}
return resource;
}
}
function addResource(resource) {
map[resource.id] = resource;
var listIndex = indexInList(resource.id);
if (listIndex === -1) {
list.push(map[resource.id])
} else {
list[listIndex] = map[resource.id];
};
}
function refreshRels(parent) {
var filterParams = {};
filterParams["_" + parent.getName() + "_parent_id"] = parent.id;
parent["_" + Resource.getName()] = $filter('filter')(list, filterParams);
$rootScope.safeApply();
}
function indexInList(id) {
var index = -1;
for (var i in list) {
if (list[i].id === id) {
index = i;
}
}
return index;
}
function timestamp(resource) {
resource.updated_at = Firebase.ServerValue.TIMESTAMP
if (!resource.created_at) {
resource.created_at = Firebase.ServerValue.TIMESTAMP
}
}
function getSaveableAttrs(resource) {
var toSave = {};
for (var key in resource) {
if (typeof(resource[key]) !== "function" &&
key.charAt(0) !== "$" &&
key !== "rels" &&
key.charAt(0) !== "_") {
toSave[key] = resource[key];
}
};
return toSave;
}
/***** Class Methods *****/
Resource.getName = function() {
return resourceRef.name();
}
Resource.getPath = function() {
return resourceRef.path.toString();
}
Resource.clearAll = function() {
map = {};
list = [];
}
Resource.find = function(id) {
return map[id];
}
Resource.findAsync = function(id) {
var deferred = $q.defer();
if (map[id]) {
$timeout(function() {
$rootScope.safeApply(function() {
deferred.resolve(map[id]);
})
}, 0)
} else {
resourceRef.child(id).once('value', function(snapshot) {
var resource = updateResource(snapshot);
map[id] = resource;
$rootScope.safeApply(function() {
deferred.resolve(map[id]);
});
});
}
return deferred.promise;
}
Resource.all = function(opts) {
var opts = opts ? opts : {}
var ret = list;
if (opts.parent) {
ret = opts.parent["_" + Resource.getName()];
}
if (opts.limit && opts.limit <= ret.length) {
ret = ret.slice(ret.length - opts.limit, ret.length);
}
return ret;
}
Resource.query = function(opts, callback) {
var opts = opts ? opts : {}
var ret = list;
var path = opts.path ? opts.path : Resource.getPath();
setListeners(path, opts, callback);
}
/***** Instance Methods *****/
Resource.prototype.getName = function() {
return Resource.getName();
}
Resource.prototype.getTimestamp = function(attr) {
var date = new Date(this[attr]);
return date.getTime();
}
Resource.prototype.save = function() {
this.beforeSave();
timestamp(this);
var deferred = $q.defer(),
newResource = false,
_this = this,
toSave = getSaveableAttrs(this),
ref = this.id ? resourceRef.child(this.id) : resourceRef.push();
if (!this.id) {
this.id = ref.name();
setAssociations(this);
newResource = true;
}
ref.update(toSave, function(error) {
$rootScope.safeApply(function() {
if (error) {
deferred.reject(error);
} else {
// addResource(_this);
if (_this._parent_rels_path && newResource) {
firebase.child(_this._parent_rels_path).once('value', function(parentRels) {
var priority = parentRels.val() ? Object.keys(parentRels.val()).length : 1;
firebase.child(_this._parent_rels_path + _this.id).setWithPriority(true, priority);
});
};
_this.afterSave();
if (newResource) {
_this.afterCreate();
};
deferred.resolve(_this);
};
});
});
return deferred.promise;
}
Resource.prototype.delete = function() {
if (this.id) {
var ref = resourceRef.child(this.id);
ref.remove();
if (this._parent_rels_path) {
firebase.child(this._parent_rels_path + this.id).remove();
}
}
}
// lifecycle callbacks - override in model
Resource.prototype.init = function() { $log.info('initializing resource') }
Resource.prototype.beforeSave = function() { $log.info('before save resource') }
Resource.prototype.afterSave = function() { $log.info('after save resource') }
Resource.prototype.afterCreate = function() { $log.info('after create resource') }
return Resource;
}
return firebaseResourceFactory;
});