1
1
var Vue // late binding
2
2
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
+
3
31
/**
4
32
* Check if a value is an object.
5
33
*
@@ -21,7 +49,7 @@ function createRecord (snapshot) {
21
49
var res = isObject ( value )
22
50
? value
23
51
: { '.value' : value }
24
- res [ '.key' ] = snapshot . key ( )
52
+ res [ '.key' ] = _getKey ( snapshot )
25
53
return res
26
54
}
27
55
@@ -61,11 +89,7 @@ function bind (vm, key, source) {
61
89
if ( ! isObject ( source ) ) {
62
90
throw new Error ( 'VueFire: invalid Firebase binding source.' )
63
91
}
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 )
69
93
vm . $firebaseRefs [ key ] = ref
70
94
vm . _firebaseSources [ key ] = source
71
95
// bind based on initial value type
@@ -94,17 +118,17 @@ function bindAsArray (vm, key, source, cancelCallback) {
94
118
} , cancelCallback )
95
119
96
120
var onRemove = source . on ( 'child_removed' , function ( snapshot ) {
97
- var index = indexForKey ( array , snapshot . key ( ) )
121
+ var index = indexForKey ( array , _getKey ( snapshot ) )
98
122
array . splice ( index , 1 )
99
123
} , cancelCallback )
100
124
101
125
var onChange = source . on ( 'child_changed' , function ( snapshot ) {
102
- var index = indexForKey ( array , snapshot . key ( ) )
126
+ var index = indexForKey ( array , _getKey ( snapshot ) )
103
127
array . splice ( index , 1 , createRecord ( snapshot ) )
104
128
} , cancelCallback )
105
129
106
130
var onMove = source . on ( 'child_moved' , function ( snapshot , prevKey ) {
107
- var index = indexForKey ( array , snapshot . key ( ) )
131
+ var index = indexForKey ( array , _getKey ( snapshot ) )
108
132
var record = array . splice ( index , 1 ) [ 0 ]
109
133
var newIndex = prevKey ? indexForKey ( array , prevKey ) + 1 : 0
110
134
array . splice ( newIndex , 0 , record )
0 commit comments