This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathangularfire.js
106 lines (105 loc) · 4.33 KB
/
angularfire.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
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
if (typeof __decorate !== "function") __decorate = function (decorators, target, key, desc) {
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
switch (arguments.length) {
case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
}
};
if (typeof __metadata !== "function") __metadata = function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
define(["require", "exports", 'angular2/angular2'], function (require, exports, angular2_1) {
var AngularFire = (function () {
function AngularFire(ref) {
this.ref = ref;
}
AngularFire.prototype.asArray = function () {
return new FirebaseArray(this.ref);
};
AngularFire = __decorate([
angular2_1.Component(),
__metadata('design:paramtypes', [(typeof Firebase !== 'undefined' && Firebase) || Object])
], AngularFire);
return AngularFire;
})();
exports.AngularFire = AngularFire;
/*
FirebaseArray
*/
var FirebaseArray = (function () {
function FirebaseArray(ref) {
this.ref = ref;
this.list = [];
// listen for changes at the Firebase instance
this.ref.on('child_added', this.created.bind(this), this.error);
this.ref.on('child_moved', this.moved.bind(this), this.error);
this.ref.on('child_changed', this.updated.bind(this), this.error);
this.ref.on('child_removed', this.removed.bind(this), this.error);
// determine when initial load is completed
// ref.once('value', function() { resolve(null); }, resolve);
}
FirebaseArray.prototype.getItem = function (recOrIndex) {
var item = recOrIndex;
if (typeof (recOrIndex) === "number") {
item = this.getRecord(recOrIndex);
}
return item;
};
FirebaseArray.prototype.getChild = function (recOrIndex) {
var item = this.getItem(recOrIndex);
return this.ref.child(item._key);
};
FirebaseArray.prototype.add = function (rec) {
this.ref.push(rec);
};
FirebaseArray.prototype.remove = function (recOrIndex) {
this.getChild(recOrIndex).remove();
};
FirebaseArray.prototype.save = function (recOrIndex) {
var item = this.getItem(recOrIndex);
this.getChild(recOrIndex).update(item);
};
FirebaseArray.prototype.keyify = function (snap) {
var item = snap.val();
item._key = snap.key();
return item;
};
FirebaseArray.prototype.created = function (snap) {
var addedValue = this.keyify(snap);
this.list.push(addedValue);
};
FirebaseArray.prototype.moved = function (snap) {
var key = snap.key();
this.spliceOut(key);
};
FirebaseArray.prototype.updated = function (snap) {
var key = snap.key();
var indexToUpdate = this.indexFor(key);
this.list[indexToUpdate] = this.keyify(snap);
};
FirebaseArray.prototype.removed = function (snap) {
var key = snap.key();
this.spliceOut(key);
};
FirebaseArray.prototype.bulkUpdate = function (items) {
this.ref.update(items);
};
FirebaseArray.prototype.spliceOut = function (key) {
var i = this.indexFor(key);
if (i > -1) {
return this.list.splice(i, 1)[0];
}
return null;
};
FirebaseArray.prototype.indexFor = function (key) {
var record = this.getRecord(key);
return this.list.indexOf(record);
};
FirebaseArray.prototype.getRecord = function (key) {
return this.list.find(function (item) { return key === item._key; });
};
return FirebaseArray;
})();
exports.FirebaseArray = FirebaseArray;
});