Skip to content

Commit db90cb0

Browse files
committed
feat(bind): automatically unbinds an already bound key
1 parent 783b656 commit db90cb0

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ function install (Vue, options) {
123123

124124
// TODO test if $bind exist and warns
125125
Vue.prototype.$bind = function (key, ref) {
126+
if (this._firestoreUnbinds[key]) {
127+
this.$unbind(key)
128+
}
126129
const promise = bind({
127130
vm: this,
128131
key,

test/bind.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,18 @@ test('rejects the promise when errors', async t => {
6464
document.onSnapshot.restore()
6565
collection.onSnapshot.restore()
6666
})
67+
68+
test('unbinds previously bound refs', async t => {
69+
const { vm, document } = t.context
70+
await document.update({ foo: 'foo' })
71+
const doc2 = db.collection().doc()
72+
await doc2.update({ bar: 'bar' })
73+
await vm.$bind('item', document)
74+
t.is(vm.$firestoreRefs.item, document)
75+
t.deepEqual(vm.item, { foo: 'foo' })
76+
await vm.$bind('item', doc2)
77+
t.deepEqual(vm.item, { bar: 'bar' })
78+
await document.update({ foo: 'baz' })
79+
t.is(vm.$firestoreRefs.item, doc2)
80+
t.deepEqual(vm.item, { bar: 'bar' })
81+
})

0 commit comments

Comments
 (0)