Skip to content

Commit

Permalink
Merge pull request #452 from firebase/jw-key-polyfill
Browse files Browse the repository at this point in the history
Update methods to Firebase 2.0.x
  • Loading branch information
katowulf committed Nov 6, 2014
2 parents a4e4671 + 6a506f9 commit cd4ef13
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
12 changes: 6 additions & 6 deletions src/FirebaseArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -513,4 +513,4 @@
return FirebaseArray;
}
]);
})();
})();
10 changes: 5 additions & 5 deletions src/FirebaseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -277,15 +277,15 @@
function ThreeWayBinding(rec) {
this.subs = [];
this.scope = null;
this.name = null;
this.key = null;
this.rec = rec;
}

ThreeWayBinding.prototype = {
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);
Expand Down Expand Up @@ -388,7 +388,7 @@
});
this.subs = [];
this.scope = null;
this.name = null;
this.key = null;
}
},

Expand All @@ -401,4 +401,4 @@
return FirebaseObject;
}
]);
})();
})();
6 changes: 3 additions & 3 deletions src/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -274,4 +274,4 @@
return AngularFire;
}
]);
})();
})();
12 changes: 11 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -401,4 +411,4 @@
});
return out;
}
})();
})();
9 changes: 8 additions & 1 deletion tests/unit/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,11 @@ describe('$firebaseUtils', function () {
});
});

});
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');
});
});

});

0 comments on commit cd4ef13

Please sign in to comment.