Skip to content

Commit 67f017f

Browse files
committed
return object for v-ref on v-repeat with object value
1 parent 61eaa27 commit 67f017f

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/directives/repeat.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ module.exports = {
209209
this.vms = this.diff(data, this.vms)
210210
// update v-ref
211211
if (this.refID) {
212-
this.vm.$[this.refID] = this.vms
212+
this.vm.$[this.refID] = this.converted
213+
? toRefObject(this.vms)
214+
: this.vms
213215
}
214216
if (this.elId) {
215217
this.vm.$$[this.elId] = this.vms.map(function (vm) {
@@ -609,4 +611,20 @@ function range (n) {
609611
ret[i] = i
610612
}
611613
return ret
614+
}
615+
616+
/**
617+
* Convert a vms array to an object ref for v-ref on an
618+
* Object value.
619+
*
620+
* @param {Array} vms
621+
* @return {Object}
622+
*/
623+
624+
function toRefObject (vms) {
625+
var ref = {}
626+
for (var i = 0, l = vms.length; i < l; i++) {
627+
ref[vms[i].$key] = vms[i]
628+
}
629+
return ref
612630
}

test/unit/specs/directives/ref_spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,31 @@ if (_.inBrowser) {
9494
})
9595
})
9696

97+
it('object v-repeat', function (done) {
98+
var vm = new Vue({
99+
el: el,
100+
data: {
101+
items: {
102+
a: 1,
103+
b: 2
104+
}
105+
},
106+
template: '<div v-repeat="items" v-ref="test"></div>'
107+
})
108+
expect(vm.$.test).toBeTruthy()
109+
expect(_.isPlainObject(vm.$.test)).toBe(true)
110+
expect(vm.$.test.a.$value).toBe(1)
111+
expect(vm.$.test.b.$value).toBe(2)
112+
vm.items = { c: 3 }
113+
_.nextTick(function () {
114+
expect(Object.keys(vm.$.test).length).toBe(1)
115+
expect(vm.$.test.c.$value).toBe(3)
116+
vm._directives[0].unbind()
117+
expect(vm.$.test).toBeNull()
118+
done()
119+
})
120+
})
121+
97122
it('nested v-repeat', function () {
98123
var vm = new Vue({
99124
el: el,

0 commit comments

Comments
 (0)