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

Commit ccf8b18

Browse files
author
jacobawenger
committed
Updated from Kato's feedback
1 parent ee6a21c commit ccf8b18

File tree

5 files changed

+28
-23
lines changed

5 files changed

+28
-23
lines changed

src/FirebaseArray.js

Lines changed: 5 additions & 9 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(this._getSnapshotKey(snap));
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 = this._getSnapshotKey(snap);
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(this._getSnapshotKey(snap));
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(this._getSnapshotKey(snap));
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(this._getSnapshotKey(snap));
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);
@@ -469,10 +469,6 @@
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();
476472
}
477473
};
478474

src/FirebaseObject.js

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

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

6161
$firebaseUtils.applyDefaults(this, this.$$defaults);
@@ -227,10 +227,6 @@
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();
234230
}
235231
};
236232

src/firebase.js

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

4444
$set: function (key, data) {
45-
var self = this;
4645
var ref = this._ref;
4746
var def = $firebaseUtils.defer();
4847
if (arguments.length > 1) {
@@ -62,8 +61,8 @@
6261
// the entire Firebase path
6362
ref.once('value', function(snap) {
6463
snap.forEach(function(ss) {
65-
if( !dataCopy.hasOwnProperty(self._getSnapshotKey(ss)) ) {
66-
dataCopy[self._getSnapshotKey(ss)] = null;
64+
if( !dataCopy.hasOwnProperty($firebaseUtils.getSnapshotKey(ss)) ) {
65+
dataCopy[$firebaseUtils.getSnapshotKey(ss)] = null;
6766
}
6867
});
6968
ref.ref().update(dataCopy, this._handle(def, ref));
@@ -173,10 +172,6 @@
173172
if (!angular.isFunction(cnf.objectFactory)) {
174173
throw new Error('config.objectFactory must be a valid function');
175174
}
176-
},
177-
178-
_getSnapshotKey: function(snapshot) {
179-
return (typeof snapshot.key === 'function') ? snapshot.key() : snapshot.name();
180175
}
181176
};
182177

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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,12 @@ 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+
181+
expect($utils.getSnapshotKey(snapshot)).toEqual('foo');
182+
});
183+
});
184+
185+
});

0 commit comments

Comments
 (0)