From cc879e57a146301f6af304a7a252bb3ecd2cbe72 Mon Sep 17 00:00:00 2001 From: Pantelis Peslis Date: Fri, 27 May 2016 15:08:59 +0300 Subject: [PATCH] Upgrade to firebase 3 --- README.md | 13 ++++++++----- package.json | 6 ++++-- src/vuefire.js | 42 +++++++++++++++++++++++++++++++++--------- tests/vuefire.spec.js | 9 ++++++--- 4 files changed, 51 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index dd89757a..fcc717e6 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,9 @@ - + - + ``` @@ -35,16 +35,19 @@ ## Usage ``` js +var firebaseApp = firebase.initializeApp({ ... }) +var db = firebaseApp.database() + var vm = new Vue({ el: '#demo', firebase: { // simple syntax, bind as an array by default - anArray: new Firebase('url/to/my/collection'), + anArray: db.ref('url/to/my/collection'), // can also bind to a query - // anArray: new Firebase('url/to/my/collection').limitToLast(25) + // anArray: db.ref('url/to/my/collection').limitToLast(25) // full syntax anObject: { - source: new Firebase('url/to/my/object'), + source: db.ref('url/to/my/object'), // optionally bind as an object asObject: true, // optionally provide the cancelCallback diff --git a/package.json b/package.json index 77b8ccf8..8145da4f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vuefire", - "version": "1.0.1", + "version": "1.1.0", "description": "Firebase bindings for Vue.js", "main": "dist/vuefire.js", "files": [ @@ -32,6 +32,9 @@ "url": "https://github.com/vuejs/vuefire/issues" }, "homepage": "https://github.com/vuejs/vuefire#readme", + "peerDependencies": { + "firebase": "^2.4.1 || 3.x.x" + }, "devDependencies": { "chai": "^3.5.0", "codecov": "^1.0.1", @@ -40,7 +43,6 @@ "eslint-plugin-html": "^1.4.0", "eslint-plugin-promise": "^1.1.0", "eslint-plugin-standard": "^1.3.2", - "firebase": "^2.4.1", "istanbul-instrumenter-loader": "^0.2.0", "karma": "^0.13.22", "karma-coverage": "^0.5.5", diff --git a/src/vuefire.js b/src/vuefire.js index 21ebb13d..9ad25a93 100644 --- a/src/vuefire.js +++ b/src/vuefire.js @@ -1,5 +1,33 @@ var Vue // late binding +/** + * Returns the key of a Firebase snapshot across SDK versions. + * + * @param {FirebaseSnapshot} snapshot + * @return {string|null} + */ +function _getKey (snapshot) { + return typeof snapshot.key === 'function' + ? snapshot.key() + : snapshot.key +} + +/** + * Returns the original reference of a Firebase reference or query across SDK versions. + * + * @param {FirebaseReference|FirebaseQuery} refOrQuery + * @return {FirebaseReference} + */ +function _getRef (refOrQuery) { + if (typeof refOrQuery.ref === 'function') { + refOrQuery = refOrQuery.ref() + } else if (typeof refOrQuery.ref === 'object') { + refOrQuery = refOrQuery.ref + } + + return refOrQuery +} + /** * Check if a value is an object. * @@ -21,7 +49,7 @@ function createRecord (snapshot) { var res = isObject(value) ? value : { '.value': value } - res['.key'] = snapshot.key() + res['.key'] = _getKey(snapshot) return res } @@ -61,11 +89,7 @@ function bind (vm, key, source) { if (!isObject(source)) { throw new Error('VueFire: invalid Firebase binding source.') } - // get the original ref for possible queries - var ref = source - if (typeof source.ref === 'function') { - ref = source.ref() - } + var ref = _getRef(source) vm.$firebaseRefs[key] = ref vm._firebaseSources[key] = source // bind based on initial value type @@ -94,17 +118,17 @@ function bindAsArray (vm, key, source, cancelCallback) { }, cancelCallback) var onRemove = source.on('child_removed', function (snapshot) { - var index = indexForKey(array, snapshot.key()) + var index = indexForKey(array, _getKey(snapshot)) array.splice(index, 1) }, cancelCallback) var onChange = source.on('child_changed', function (snapshot) { - var index = indexForKey(array, snapshot.key()) + var index = indexForKey(array, _getKey(snapshot)) array.splice(index, 1, createRecord(snapshot)) }, cancelCallback) var onMove = source.on('child_moved', function (snapshot, prevKey) { - var index = indexForKey(array, snapshot.key()) + var index = indexForKey(array, _getKey(snapshot)) var record = array.splice(index, 1)[0] var newIndex = prevKey ? indexForKey(array, prevKey) + 1 : 0 array.splice(newIndex, 0, record) diff --git a/tests/vuefire.spec.js b/tests/vuefire.spec.js index f7e44ae6..61eeacd4 100644 --- a/tests/vuefire.spec.js +++ b/tests/vuefire.spec.js @@ -5,13 +5,16 @@ var helpers = require('./helpers') Vue.use(VueFire) -var demoFirebaseUrl = 'https://' + helpers.generateRandomString() + '.firebaseio-demo.com' +var firebaseApp = Firebase.initializeApp({ + apiKey: helpers.generateRandomString(), + databaseURL: 'https://' + helpers.generateRandomString() + '.firebaseio-demo.com' +}) describe('VueFire', function () { var firebaseRef beforeEach(function (done) { - firebaseRef = new Firebase(demoFirebaseUrl) + firebaseRef = firebaseApp.database().ref() firebaseRef.remove(function (error) { if (error) { done(error) @@ -595,7 +598,7 @@ describe('VueFire', function () { }) it('sets the key as null when bound to the root of the database', function (done) { - var rootRef = firebaseRef.root() + var rootRef = firebaseRef.root var vm = new Vue({ firebase: { items: {