Skip to content

Commit

Permalink
Merge branch 'upgrade' of git://github.com/pespantelis/vuefire into 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 17, 2016
2 parents 9571a24 + cc879e5 commit 6eba17a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<!-- Vue -->
<script src="https://cdn.jsdelivr.net/vue/1.0.24/vue.js"></script>
<!-- Firebase -->
<script src="https://cdn.firebase.com/js/client/2.4.2/firebase.js"></script>
<script src="https://gstatic.com/firebasejs/3.0.3/firebase.js"></script>
<!-- VueFire -->
<script src="https://cdn.jsdelivr.net/vuefire/1.0.0/vuefire.min.js"></script>
<script src="https://cdn.jsdelivr.net/vuefire/1.1.0/vuefire.min.js"></script>
</head>
```

Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
42 changes: 33 additions & 9 deletions src/vuefire.js
Original file line number Diff line number Diff line change
@@ -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.
*
Expand All @@ -21,7 +49,7 @@ function createRecord (snapshot) {
var res = isObject(value)
? value
: { '.value': value }
res['.key'] = snapshot.key()
res['.key'] = _getKey(snapshot)
return res
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions tests/vuefire.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit 6eba17a

Please sign in to comment.