Skip to content
This repository was archived by the owner on May 12, 2024. It is now read-only.

Commit a23b4ce

Browse files
committed
Avoid defining reactive twice
Fix vuejs#19
1 parent fd2b58e commit a23b4ce

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

src/vuefire.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,22 @@ function bind (vm, key, source) {
100100
}
101101
}
102102

103+
/**
104+
* Define a reactive property in a given vm if it's not defined
105+
* yet
106+
*
107+
* @param {Vue} vm
108+
* @param {string} key
109+
* @param {*} val
110+
*/
111+
function defineReactive (vm, key, val) {
112+
if (vm[key] === undefined) {
113+
Vue.util.defineReactive(vm, key, val)
114+
} else {
115+
vm[key] = val
116+
}
117+
}
118+
103119
/**
104120
* Bind a firebase data source to a key on a vm as an Array.
105121
*
@@ -110,7 +126,7 @@ function bind (vm, key, source) {
110126
*/
111127
function bindAsArray (vm, key, source, cancelCallback) {
112128
var array = []
113-
Vue.util.defineReactive(vm, key, array)
129+
defineReactive(vm, key, array)
114130

115131
var onAdd = source.on('child_added', function (snapshot, prevKey) {
116132
var index = prevKey ? indexForKey(array, prevKey) + 1 : 0
@@ -151,7 +167,7 @@ function bindAsArray (vm, key, source, cancelCallback) {
151167
* @param {function|null} cancelCallback
152168
*/
153169
function bindAsObject (vm, key, source, cancelCallback) {
154-
Vue.util.defineReactive(vm, key, {})
170+
defineReactive(vm, key, {})
155171
var cb = source.on('value', function (snapshot) {
156172
vm[key] = createRecord(snapshot)
157173
}, cancelCallback)

tests/vuefire.spec.js

+64
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,41 @@ describe('VueFire', function () {
8686
})
8787
})
8888

89+
it('bind using $bindAsArray after $unbind', function (done) {
90+
var vm = new Vue({
91+
template: '<div><div v-for="item in items">{{ item[".key"] }} {{ item.index }} </div></div>',
92+
created: function () {
93+
this.$bindAsArray('items', firebaseRef)
94+
}
95+
}).$mount()
96+
var firebaseRefOther = firebaseApp.database().ref().child(helpers.generateRandomString())
97+
firebaseRef.set({
98+
first: { index: 0 },
99+
second: { index: 1 },
100+
third: { index: 2 }
101+
}, function () {
102+
expect(vm.items).to.deep.equal([
103+
{ '.key': 'first', index: 0 },
104+
{ '.key': 'second', index: 1 },
105+
{ '.key': 'third', index: 2 }
106+
])
107+
vm.$unbind('items')
108+
vm.$bindAsArray('items', firebaseRefOther)
109+
firebaseRefOther.set({
110+
a: { index: 0 },
111+
b: { index: 1 },
112+
c: { index: 2 }
113+
}, function () {
114+
expect(vm.items).to.deep.equal([
115+
{ '.key': 'a', index: 0 },
116+
{ '.key': 'b', index: 1 },
117+
{ '.key': 'c', index: 2 }
118+
])
119+
done()
120+
})
121+
})
122+
})
123+
89124
it('binds array records which are primitives', function (done) {
90125
var vm = new Vue({
91126
firebase: {
@@ -531,6 +566,35 @@ describe('VueFire', function () {
531566
})
532567
})
533568

569+
it('binds with $bindAsObject after $unbind', function (done) {
570+
var obj = {
571+
first: { index: 0 },
572+
second: { index: 1 },
573+
third: { index: 2 }
574+
}
575+
var objOther = {
576+
onlyOne: { index: 0 },
577+
second: { index: 1 }
578+
}
579+
var vm = new Vue({
580+
template: '<div>{{ items | json }}</div>',
581+
created: function () {
582+
this.$bindAsObject('items', firebaseRef.child('items'))
583+
}
584+
}).$mount()
585+
firebaseRef.child('items').set(obj, function () {
586+
obj['.key'] = 'items'
587+
expect(vm.items).to.deep.equal(obj)
588+
vm.$unbind('items')
589+
vm.$bindAsObject('items', firebaseRef.child('others'))
590+
firebaseRef.child('others').set(objOther, function () {
591+
objOther['.key'] = 'others'
592+
expect(vm.items).to.deep.equal(objOther)
593+
done()
594+
})
595+
})
596+
})
597+
534598
it('binds with $bindAsObject', function (done) {
535599
var obj = {
536600
first: { index: 0 },

0 commit comments

Comments
 (0)