diff --git a/src/FirebaseArray.js b/src/FirebaseArray.js index 6d09511a..180d5d97 100644 --- a/src/FirebaseArray.js +++ b/src/FirebaseArray.js @@ -256,14 +256,14 @@ */ $$added: function(snap, prevChild) { // check to make sure record does not exist - var i = this.$indexFor(snap.name()); + var i = this.$indexFor($firebaseUtils.getSnapshotKey(snap)); if( i === -1 ) { // parse data and create record var rec = snap.val(); if( !angular.isObject(rec) ) { rec = { $value: rec }; } - rec.$id = snap.name(); + rec.$id = $firebaseUtils.getSnapshotKey(snap); rec.$priority = snap.getPriority(); $firebaseUtils.applyDefaults(rec, this.$$defaults); @@ -279,7 +279,7 @@ * @param snap */ $$removed: function(snap) { - var rec = this.$getRecord(snap.name()); + var rec = this.$getRecord($firebaseUtils.getSnapshotKey(snap)); if( angular.isObject(rec) ) { this._process('child_removed', rec); } @@ -292,7 +292,7 @@ * @param snap */ $$updated: function(snap) { - var rec = this.$getRecord(snap.name()); + var rec = this.$getRecord($firebaseUtils.getSnapshotKey(snap)); if( angular.isObject(rec) ) { // apply changes to the record var changed = $firebaseUtils.updateRec(rec, snap); @@ -311,7 +311,7 @@ * @param {string} prevChild */ $$moved: function(snap, prevChild) { - var rec = this.$getRecord(snap.name()); + var rec = this.$getRecord($firebaseUtils.getSnapshotKey(snap)); if( angular.isObject(rec) ) { rec.$priority = snap.getPriority(); this._process('child_moved', rec, prevChild); @@ -513,4 +513,4 @@ return FirebaseArray; } ]); -})(); \ No newline at end of file +})(); diff --git a/src/FirebaseObject.js b/src/FirebaseObject.js index 5067cfdc..1ec72a07 100644 --- a/src/FirebaseObject.js +++ b/src/FirebaseObject.js @@ -55,7 +55,7 @@ value: this.$$conf }); - this.$id = $firebase.$ref().ref().name(); + this.$id = $firebaseUtils.getSnapshotKey($firebase.$ref().ref()); this.$priority = null; $firebaseUtils.applyDefaults(this, this.$$defaults); @@ -277,7 +277,7 @@ function ThreeWayBinding(rec) { this.subs = []; this.scope = null; - this.name = null; + this.key = null; this.rec = rec; } @@ -285,7 +285,7 @@ assertNotBound: function(varName) { if( this.scope ) { var msg = 'Cannot bind to ' + varName + ' because this instance is already bound to ' + - this.name + '; one binding per instance ' + + this.key + '; one binding per instance ' + '(call unbind method or create another $firebase instance)'; $log.error(msg); return $firebaseUtils.reject(msg); @@ -388,7 +388,7 @@ }); this.subs = []; this.scope = null; - this.name = null; + this.key = null; } }, @@ -401,4 +401,4 @@ return FirebaseObject; } ]); -})(); \ No newline at end of file +})(); diff --git a/src/firebase.js b/src/firebase.js index f76c2bbf..6199cfef 100644 --- a/src/firebase.js +++ b/src/firebase.js @@ -61,8 +61,8 @@ // the entire Firebase path ref.once('value', function(snap) { snap.forEach(function(ss) { - if( !dataCopy.hasOwnProperty(ss.name()) ) { - dataCopy[ss.name()] = null; + if( !dataCopy.hasOwnProperty($firebaseUtils.getSnapshotKey(ss)) ) { + dataCopy[$firebaseUtils.getSnapshotKey(ss)] = null; } }); ref.ref().update(dataCopy, this._handle(def, ref)); @@ -274,4 +274,4 @@ return AngularFire; } ]); -})(); \ No newline at end of file +})(); diff --git a/src/utils.js b/src/utils.js index a95d8558..db00f636 100644 --- a/src/utils.js +++ b/src/utils.js @@ -341,6 +341,16 @@ return obj; }, + /** + * A utility for retrieving a DataSnapshot's key name. This + * is backwards-compatible with `name()` from Firebase 1.x.x + * and `key()` from Firebase 2.0.0+. Once support for Firebase + * 1.x.x is dropped in AngularFire, this helper can be removed. + */ + getSnapshotKey: function(snapshot) { + return (typeof snapshot.key === 'function') ? snapshot.key() : snapshot.name(); + }, + /** * A utility for converting records to JSON objects * which we can save into Firebase. It asserts valid @@ -401,4 +411,4 @@ }); return out; } -})(); \ No newline at end of file +})(); diff --git a/tests/unit/utils.spec.js b/tests/unit/utils.spec.js index 58815720..54ea2173 100644 --- a/tests/unit/utils.spec.js +++ b/tests/unit/utils.spec.js @@ -174,4 +174,11 @@ describe('$firebaseUtils', function () { }); }); -}); \ No newline at end of file + describe('#getSnapshotKey', function() { + it('should return the key name given a DataSnapshot', function() { + var snapshot = testutils.snap('data', 'foo'); + expect($utils.getSnapshotKey(snapshot)).toEqual('foo'); + }); + }); + +});