11var Vue // late binding
22
3+ /**
4+ * Returns the key of a Firebase snapshot across SDK versions.
5+ *
6+ * @param {FirebaseSnapshot } snapshot
7+ * @return {string|null }
8+ */
9+ function _getKey ( snapshot ) {
10+ return typeof snapshot . key === 'function'
11+ ? snapshot . key ( )
12+ : snapshot . key
13+ }
14+
15+ /**
16+ * Returns the original reference of a Firebase reference or query across SDK versions.
17+ *
18+ * @param {FirebaseReference|FirebaseQuery } refOrQuery
19+ * @return {FirebaseReference }
20+ */
21+ function _getRef ( refOrQuery ) {
22+ if ( typeof refOrQuery . ref === 'function' ) {
23+ refOrQuery = refOrQuery . ref ( )
24+ } else if ( typeof refOrQuery . ref === 'object' ) {
25+ refOrQuery = refOrQuery . ref
26+ }
27+
28+ return refOrQuery
29+ }
30+
331/**
432 * Check if a value is an object.
533 *
@@ -21,7 +49,7 @@ function createRecord (snapshot) {
2149 var res = isObject ( value )
2250 ? value
2351 : { '.value' : value }
24- res [ '.key' ] = snapshot . key ( )
52+ res [ '.key' ] = _getKey ( snapshot )
2553 return res
2654}
2755
@@ -61,11 +89,7 @@ function bind (vm, key, source) {
6189 if ( ! isObject ( source ) ) {
6290 throw new Error ( 'VueFire: invalid Firebase binding source.' )
6391 }
64- // get the original ref for possible queries
65- var ref = source
66- if ( typeof source . ref === 'function' ) {
67- ref = source . ref ( )
68- }
92+ var ref = _getRef ( source )
6993 vm . $firebaseRefs [ key ] = ref
7094 vm . _firebaseSources [ key ] = source
7195 // bind based on initial value type
@@ -94,17 +118,17 @@ function bindAsArray (vm, key, source, cancelCallback) {
94118 } , cancelCallback )
95119
96120 var onRemove = source . on ( 'child_removed' , function ( snapshot ) {
97- var index = indexForKey ( array , snapshot . key ( ) )
121+ var index = indexForKey ( array , _getKey ( snapshot ) )
98122 array . splice ( index , 1 )
99123 } , cancelCallback )
100124
101125 var onChange = source . on ( 'child_changed' , function ( snapshot ) {
102- var index = indexForKey ( array , snapshot . key ( ) )
126+ var index = indexForKey ( array , _getKey ( snapshot ) )
103127 array . splice ( index , 1 , createRecord ( snapshot ) )
104128 } , cancelCallback )
105129
106130 var onMove = source . on ( 'child_moved' , function ( snapshot , prevKey ) {
107- var index = indexForKey ( array , snapshot . key ( ) )
131+ var index = indexForKey ( array , _getKey ( snapshot ) )
108132 var record = array . splice ( index , 1 ) [ 0 ]
109133 var newIndex = prevKey ? indexForKey ( array , prevKey ) + 1 : 0
110134 array . splice ( newIndex , 0 , record )
0 commit comments