diff --git a/bower.json b/bower.json index 1afc462..c2aca43 100644 --- a/bower.json +++ b/bower.json @@ -32,7 +32,7 @@ "index.html" ], "dependencies": { - "firebase": "1.1.x", + "firebase": "2.0.x", "backbone": "<=1.1.0", "underscore": "~1.5.2" }, diff --git a/changelog.txt b/changelog.txt index cc5eb9a..e6f1d52 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1 +1 @@ -feature - Upgraded Firebase dependency to 1.1.x. +feature - Upgraded Firebase dependency to 2.0.x. \ No newline at end of file diff --git a/package.json b/package.json index f66ba3d..085e140 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "package.json" ], "dependencies": { - "firebase": "1.1.x" + "firebase": "2.0.x" }, "devDependencies": { "chai-backbone": "~0.9.2", diff --git a/src/backfire.js b/src/backfire.js index ed72848..ad78c4f 100644 --- a/src/backfire.js +++ b/src/backfire.js @@ -30,9 +30,21 @@ }; _.extend(Backbone.Firebase.prototype, { + + /** + * A utility for retrieving the key name of a Firebase ref or + * DataSnapshot. 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 BackFire, this + * helper can be removed. + */ + _getKey: function(refOrSnapshot) { + return (typeof refOrSnapshot.key === 'function') ? refOrSnapshot.key() : refOrSnapshot.name(); + }, + _childAdded: function(childSnap, prevChild) { var model = childSnap.val(); - model.id = childSnap.name(); + model.id = this._getKey(childSnap); if (prevChild) { var item = _.find(this._children, function(child) { return child.id == prevChild; @@ -53,7 +65,7 @@ _childChanged: function(childSnap) { var model = childSnap.val(); - model.id = childSnap.name(); + model.id = this._getKey(childSnap); var item = _.find(this._children, function(child) { return child.id == model.id; }); @@ -69,7 +81,7 @@ create: function(model, cb) { if (!model.id) { - model.id = this._fbref.ref().push().name(); + model.id = this._getKey(this._fbref.ref().push()); } var val = model.toJSON(); @@ -306,7 +318,7 @@ model = model.toJSON(); } if (!model.id) { - model.id = this.firebase.ref().push().name(); + model.id = Backbone.Firebase.prototype._getKey(this.firebase.ref().push()); } ret.push(model); } @@ -319,7 +331,7 @@ if (!_.isObject(model)) { model = {}; } - model.id = snap.name(); + model.id = Backbone.Firebase.prototype._getKey(snap); } if (this._suppressEvent === true) { this._suppressEvent = false; @@ -338,7 +350,7 @@ _childChanged: function(snap) { var model = snap.val(); if (!model.id) { - model.id = snap.name(); + model.id = Backbone.Firebase.prototype._getKey(snap); } var item = _.find(this.models, function(child) { @@ -365,7 +377,7 @@ _childRemoved: function(snap) { var model = snap.val(); if (!model.id) { - model.id = snap.name(); + model.id = Backbone.Firebase.prototype._getKey(snap); } if (this._suppressEvent === true) { this._suppressEvent = false;