Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit ee6a21c

Browse files
author
jacobawenger
committed
Update methods to Firebase 2.0.x
1 parent a4e4671 commit ee6a21c

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

src/FirebaseArray.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,14 @@
256256
*/
257257
$$added: function(snap, prevChild) {
258258
// check to make sure record does not exist
259-
var i = this.$indexFor(snap.name());
259+
var i = this.$indexFor(this._getSnapshotKey(snap));
260260
if( i === -1 ) {
261261
// parse data and create record
262262
var rec = snap.val();
263263
if( !angular.isObject(rec) ) {
264264
rec = { $value: rec };
265265
}
266-
rec.$id = snap.name();
266+
rec.$id = this._getSnapshotKey(snap);
267267
rec.$priority = snap.getPriority();
268268
$firebaseUtils.applyDefaults(rec, this.$$defaults);
269269

@@ -279,7 +279,7 @@
279279
* @param snap
280280
*/
281281
$$removed: function(snap) {
282-
var rec = this.$getRecord(snap.name());
282+
var rec = this.$getRecord(this._getSnapshotKey(snap));
283283
if( angular.isObject(rec) ) {
284284
this._process('child_removed', rec);
285285
}
@@ -292,7 +292,7 @@
292292
* @param snap
293293
*/
294294
$$updated: function(snap) {
295-
var rec = this.$getRecord(snap.name());
295+
var rec = this.$getRecord(this._getSnapshotKey(snap));
296296
if( angular.isObject(rec) ) {
297297
// apply changes to the record
298298
var changed = $firebaseUtils.updateRec(rec, snap);
@@ -311,7 +311,7 @@
311311
* @param {string} prevChild
312312
*/
313313
$$moved: function(snap, prevChild) {
314-
var rec = this.$getRecord(snap.name());
314+
var rec = this.$getRecord(this._getSnapshotKey(snap));
315315
if( angular.isObject(rec) ) {
316316
rec.$priority = snap.getPriority();
317317
this._process('child_moved', rec, prevChild);
@@ -469,6 +469,10 @@
469469
if( this._isDestroyed ) {
470470
throw new Error('Cannot call ' + method + ' method on a destroyed $FirebaseArray object');
471471
}
472+
},
473+
474+
_getSnapshotKey: function(snapshot) {
475+
return (typeof snapshot.key === 'function') ? snapshot.key() : snapshot.name();
472476
}
473477
};
474478

@@ -513,4 +517,4 @@
513517
return FirebaseArray;
514518
}
515519
]);
516-
})();
520+
})();

src/FirebaseObject.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
value: this.$$conf
5656
});
5757

58-
this.$id = $firebase.$ref().ref().name();
58+
this.$id = this._getSnapshotKey($firebase.$ref().ref());
5959
this.$priority = null;
6060

6161
$firebaseUtils.applyDefaults(this, this.$$defaults);
@@ -227,6 +227,10 @@
227227
*/
228228
forEach: function(iterator, context) {
229229
return $firebaseUtils.each(this, iterator, context);
230+
},
231+
232+
_getSnapshotKey: function(snapshot) {
233+
return (typeof snapshot.key === 'function') ? snapshot.key() : snapshot.name();
230234
}
231235
};
232236

@@ -277,15 +281,15 @@
277281
function ThreeWayBinding(rec) {
278282
this.subs = [];
279283
this.scope = null;
280-
this.name = null;
284+
this.key = null;
281285
this.rec = rec;
282286
}
283287

284288
ThreeWayBinding.prototype = {
285289
assertNotBound: function(varName) {
286290
if( this.scope ) {
287291
var msg = 'Cannot bind to ' + varName + ' because this instance is already bound to ' +
288-
this.name + '; one binding per instance ' +
292+
this.key + '; one binding per instance ' +
289293
'(call unbind method or create another $firebase instance)';
290294
$log.error(msg);
291295
return $firebaseUtils.reject(msg);
@@ -388,7 +392,7 @@
388392
});
389393
this.subs = [];
390394
this.scope = null;
391-
this.name = null;
395+
this.key = null;
392396
}
393397
},
394398

@@ -401,4 +405,4 @@
401405
return FirebaseObject;
402406
}
403407
]);
404-
})();
408+
})();

src/firebase.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
},
4343

4444
$set: function (key, data) {
45+
var self = this;
4546
var ref = this._ref;
4647
var def = $firebaseUtils.defer();
4748
if (arguments.length > 1) {
@@ -61,8 +62,8 @@
6162
// the entire Firebase path
6263
ref.once('value', function(snap) {
6364
snap.forEach(function(ss) {
64-
if( !dataCopy.hasOwnProperty(ss.name()) ) {
65-
dataCopy[ss.name()] = null;
65+
if( !dataCopy.hasOwnProperty(self._getSnapshotKey(ss)) ) {
66+
dataCopy[self._getSnapshotKey(ss)] = null;
6667
}
6768
});
6869
ref.ref().update(dataCopy, this._handle(def, ref));
@@ -172,6 +173,10 @@
172173
if (!angular.isFunction(cnf.objectFactory)) {
173174
throw new Error('config.objectFactory must be a valid function');
174175
}
176+
},
177+
178+
_getSnapshotKey: function(snapshot) {
179+
return (typeof snapshot.key === 'function') ? snapshot.key() : snapshot.name();
175180
}
176181
};
177182

@@ -274,4 +279,4 @@
274279
return AngularFire;
275280
}
276281
]);
277-
})();
282+
})();

0 commit comments

Comments
 (0)