forked from FirebaseExtended/emberfire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emberfire-latest.js
178 lines (140 loc) · 3.68 KB
/
emberfire-latest.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
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
"use strict";
var EmberFire = Ember.Namespace.create();
EmberFire.coerce = function(snapshot) {
var object = snapshot.val(),
ref = snapshot.ref(),
type = object._type;
switch (type) {
case "object":
object = EmberFire.Object.create({ ref: ref });
break;
case "array":
object = EmberFire.Array.create({ ref: ref });
break;
case "objectArray":
object = EmberFire.ObjectArray.create({ ref: ref });
break;
}
return object;
};
EmberFire.ObjectMixin = Ember.Mixin.create({
type: null,
typeKey: "_type",
init: function() {
this.ref.child(this.typeKey).set(this.type);
this.ref.on("child_added", function(snapshot) {
if (snapshot.name() != this.typeKey) {
this.childAdded(snapshot);
}
}, this);
this.ref.on("child_removed", function(snapshot) {
if (snapshot.name() != this.typeKey) {
this.childRemoved(snapshot);
}
}, this);
this.ref.on("child_changed", function(snapshot) {
if (snapshot.name() != this.typeKey) {
this.childChanged(snapshot);
}
}, this);
this.ref.on("value", function(snapshot) {
if (snapshot.name() != this.typeKey) {
this.valueChanged(snapshot);
}
}, this);
this._super();
},
childAdded: function(){
},
childChanged: function(){
},
childRemoved: function(){
},
valueChanged: function(){
},
willDestroy: function() {
this.ref.off();
}
});
EmberFire.Object = Ember.ObjectProxy.extend(EmberFire.ObjectMixin, {
type: "object",
ref: null,
init: function() {
this.set("content", {});
this._super();
},
valueChanged: function(snapshot){
this.set("content", snapshot.val());
},
toJSON: function() {
var json = {},
object = this.get("content");
for (var key in object) {
json[key] = Ember.get(object, key);
}
json._type = "object";
return json;
},
setUnknownProperty: function(key, value) {
this.ref.child(key).set(value);
return this._super(key, value);
}
});
EmberFire.Array = Ember.ArrayProxy.extend(EmberFire.ObjectMixin, {
type: "array",
coerceChild: EmberFire.coerce,
init: function() {
this._array = Ember.A([]);
this._index = Ember.A([]);
this.set("content", this._array);
this._super();
},
childAdded: function(snapshot) {
var object = this.coerceChild(snapshot),
key = snapshot.name();
this._index.pushObject(key);
this._array.pushObject(object);
},
childRemoved: function(snapshot) {
var idx = this._index.indexOf(snapshot.name());
this._index.removeAt(idx);
this._array.removeAt(idx);
},
childChanged: function(snapshot) {
var idx = this._index.indexOf(snapshot.name()),
existing = this._array.objectAt(idx),
isObject = (existing instanceof EmberFire.Object);
if (!isObject) {
var object = this.coerceChild(snapshot);
this._array.replace(idx, 1, [object]);
}
},
replaceContent: function(idx, amt, objects) {
for (var i = 0; i < amt; i++) {
var key = this._index[idx+i];
this.ref.child(key).remove();
}
objects.forEach(function(object) {
var val = object;
if (object.toJSON) {
val = object.toJSON();
}
return this.ref.push(val).name();
}, this);
},
toJSON: function() {
var json = {},
values = this.get("content");
for (var i = 0; i < this._index.length; i++) {
json[this._index[i]] = values[i];
}
json._type = this.type;
return json;
}
});
EmberFire.ObjectArray = EmberFire.Array.extend({
type: "objectArray",
coerceChild: function(snapshot) {
return EmberFire.Object.create({ ref: snapshot.ref() });
}
});