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

Commit cd4ef13

Browse files
committed
Merge pull request #452 from firebase/jw-key-polyfill
Update methods to Firebase 2.0.x
2 parents a4e4671 + 6a506f9 commit cd4ef13

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

src/FirebaseArray.js

Lines changed: 6 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($firebaseUtils.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 = $firebaseUtils.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($firebaseUtils.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($firebaseUtils.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($firebaseUtils.getSnapshotKey(snap));
315315
if( angular.isObject(rec) ) {
316316
rec.$priority = snap.getPriority();
317317
this._process('child_moved', rec, prevChild);
@@ -513,4 +513,4 @@
513513
return FirebaseArray;
514514
}
515515
]);
516-
})();
516+
})();

src/FirebaseObject.js

Lines changed: 5 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 = $firebaseUtils.getSnapshotKey($firebase.$ref().ref());
5959
this.$priority = null;
6060

6161
$firebaseUtils.applyDefaults(this, this.$$defaults);
@@ -277,15 +277,15 @@
277277
function ThreeWayBinding(rec) {
278278
this.subs = [];
279279
this.scope = null;
280-
this.name = null;
280+
this.key = null;
281281
this.rec = rec;
282282
}
283283

284284
ThreeWayBinding.prototype = {
285285
assertNotBound: function(varName) {
286286
if( this.scope ) {
287287
var msg = 'Cannot bind to ' + varName + ' because this instance is already bound to ' +
288-
this.name + '; one binding per instance ' +
288+
this.key + '; one binding per instance ' +
289289
'(call unbind method or create another $firebase instance)';
290290
$log.error(msg);
291291
return $firebaseUtils.reject(msg);
@@ -388,7 +388,7 @@
388388
});
389389
this.subs = [];
390390
this.scope = null;
391-
this.name = null;
391+
this.key = null;
392392
}
393393
},
394394

@@ -401,4 +401,4 @@
401401
return FirebaseObject;
402402
}
403403
]);
404-
})();
404+
})();

src/firebase.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@
6161
// the entire Firebase path
6262
ref.once('value', function(snap) {
6363
snap.forEach(function(ss) {
64-
if( !dataCopy.hasOwnProperty(ss.name()) ) {
65-
dataCopy[ss.name()] = null;
64+
if( !dataCopy.hasOwnProperty($firebaseUtils.getSnapshotKey(ss)) ) {
65+
dataCopy[$firebaseUtils.getSnapshotKey(ss)] = null;
6666
}
6767
});
6868
ref.ref().update(dataCopy, this._handle(def, ref));
@@ -274,4 +274,4 @@
274274
return AngularFire;
275275
}
276276
]);
277-
})();
277+
})();

src/utils.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,16 @@
341341
return obj;
342342
},
343343

344+
/**
345+
* A utility for retrieving a DataSnapshot's key name. This
346+
* is backwards-compatible with `name()` from Firebase 1.x.x
347+
* and `key()` from Firebase 2.0.0+. Once support for Firebase
348+
* 1.x.x is dropped in AngularFire, this helper can be removed.
349+
*/
350+
getSnapshotKey: function(snapshot) {
351+
return (typeof snapshot.key === 'function') ? snapshot.key() : snapshot.name();
352+
},
353+
344354
/**
345355
* A utility for converting records to JSON objects
346356
* which we can save into Firebase. It asserts valid
@@ -401,4 +411,4 @@
401411
});
402412
return out;
403413
}
404-
})();
414+
})();

tests/unit/utils.spec.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,11 @@ describe('$firebaseUtils', function () {
174174
});
175175
});
176176

177-
});
177+
describe('#getSnapshotKey', function() {
178+
it('should return the key name given a DataSnapshot', function() {
179+
var snapshot = testutils.snap('data', 'foo');
180+
expect($utils.getSnapshotKey(snapshot)).toEqual('foo');
181+
});
182+
});
183+
184+
});

0 commit comments

Comments
 (0)