Skip to content

Commit 7667fe5

Browse files
committed
feat(unbind): manually unbind a ref
1 parent 250a4f9 commit 7667fe5

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/index.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,9 @@ function install (Vue, options) {
116116
}
117117

118118
Vue.prototype.$unbind = function (key) {
119-
unbind({
120-
vm: this,
121-
key,
122-
})
119+
this._firestoreUnbinds[key]()
120+
delete this._firestoreUnbinds[key]
121+
delete this.$firestoreRefs[key]
123122
}
124123
}
125124

test/unbind.spec.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import test from 'ava'
2+
import sinon from 'sinon'
3+
import Vuefire from '../src'
4+
import {
5+
createSnapshot
6+
} from '../src/utils'
7+
import {
8+
db,
9+
tick,
10+
Vue
11+
} from './helpers'
12+
13+
Vue.use(Vuefire)
14+
15+
test.beforeEach(async t => {
16+
t.context.collection = db.collection()
17+
t.context.document = t.context.collection.doc()
18+
t.context.vm = new Vue({
19+
render (h) {
20+
return h('ul', this.items && this.items.map(
21+
item => h('li', [item])
22+
))
23+
},
24+
// purposely set items as null
25+
// but it's a good practice to set it to an empty array
26+
data: () => ({
27+
items: null,
28+
item: null,
29+
}),
30+
firestore: {
31+
items: t.context.collection,
32+
item: t.context.document
33+
}
34+
}).$mount()
35+
await tick()
36+
})
37+
38+
test('manually unbinds a collection', async t => {
39+
const vm = t.context.vm
40+
const spy = sinon.spy(vm._firestoreUnbinds, 'items')
41+
vm.$unbind('items')
42+
t.is(spy.callCount, 1)
43+
t.deepEqual(Object.keys(vm._firestoreUnbinds), ['item'])
44+
t.deepEqual(Object.keys(vm.$firestoreRefs), ['item'])
45+
t.deepEqual(vm.items, [])
46+
await t.context.collection.add({ text: 'foo' })
47+
t.deepEqual(vm.items, [])
48+
})

0 commit comments

Comments
 (0)