File tree Expand file tree Collapse file tree 2 files changed +68
-15
lines changed Expand file tree Collapse file tree 2 files changed +68
-15
lines changed Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ function bindDocument ({
44
44
45
45
const unbind = document . onSnapshot ( ( doc ) => {
46
46
// TODO test doc.exist
47
- console . log ( 'doc data' , doc )
47
+ // console.log('doc data', doc)
48
48
const [ data , refs ] = extractRefs ( createSnapshot ( doc ) )
49
49
vm [ key ] = data
50
50
return
@@ -66,29 +66,41 @@ function bindDocument ({
66
66
}
67
67
}
68
68
69
+ function bind ( { vm, key, ref } ) {
70
+ if ( ref . add ) {
71
+ bindCollection ( {
72
+ vm,
73
+ key,
74
+ collection : ref ,
75
+ } )
76
+ } else {
77
+ bindDocument ( {
78
+ vm,
79
+ key,
80
+ document : ref ,
81
+ } )
82
+ }
83
+ }
84
+
69
85
function install ( Vue , options ) {
70
86
Vue . mixin ( {
71
87
created ( ) {
72
88
const { firestore } = this . $options
73
89
if ( ! firestore ) return
74
90
Object . keys ( firestore ) . forEach ( key => {
75
- const ref = firestore [ key ]
76
- if ( ref . add ) {
77
- bindCollection ( {
78
- vm : this ,
79
- key,
80
- collection : ref ,
81
- } )
82
- } else {
83
- bindDocument ( {
84
- vm : this ,
85
- key,
86
- document : ref ,
87
- } )
88
- }
91
+ this . $bind ( key , firestore [ key ] )
89
92
} )
90
93
}
91
94
} )
95
+
96
+ // TODO test if $bind exist and warns
97
+ Vue . prototype . $bind = function ( key , ref ) {
98
+ bind ( {
99
+ vm : this ,
100
+ key,
101
+ ref,
102
+ } )
103
+ }
92
104
}
93
105
94
106
export default install
Original file line number Diff line number Diff line change
1
+ import test from 'ava'
2
+ import Vuefire from '../src'
3
+ import {
4
+ createSnapshot
5
+ } from '../src/utils'
6
+ import {
7
+ db ,
8
+ tick ,
9
+ Vue
10
+ } from './helpers'
11
+
12
+ Vue . use ( Vuefire )
13
+
14
+ test . beforeEach ( async t => {
15
+ t . context . collection = db . collection ( )
16
+ t . context . document = t . context . collection . doc ( )
17
+ t . context . vm = new Vue ( {
18
+ render ( h ) {
19
+ return h ( 'ul' , this . items && this . items . map (
20
+ item => h ( 'li' , [ item ] )
21
+ ) )
22
+ } ,
23
+ // purposely set items as null
24
+ // but it's a good practice to set it to an empty array
25
+ data : ( ) => ( {
26
+ items : null ,
27
+ item : null ,
28
+ } ) ,
29
+ } ) . $mount ( )
30
+ await tick ( )
31
+ } )
32
+
33
+ test ( 'manually binds a collection' , async t => {
34
+ const vm = t . context . vm
35
+ const collection = t . context . collection
36
+ t . deepEqual ( vm . items , null )
37
+ await vm . $bind ( 'items' , collection )
38
+ t . deepEqual ( vm . items , [ ] )
39
+ await collection . add ( { text : 'foo' } )
40
+ t . deepEqual ( vm . items , [ { text : 'foo' } ] )
41
+ } )
You can’t perform that action at this time.
0 commit comments