Skip to content

Commit 1617cd9

Browse files
authored
Merge pull request #22 from posva/fix-array-rebind
Fix array rebind
2 parents 3def9d5 + 14bc018 commit 1617cd9

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-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 (key in vm) {
113+
vm[key] = val
114+
} else {
115+
Vue.util.defineReactive(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

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

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

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

0 commit comments

Comments
 (0)