Skip to content

Commit e8c36e1

Browse files
committed
sync v-repeat $value back to original object/array (fix #761)
1 parent 67eb247 commit e8c36e1

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/directives/repeat.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,16 @@ module.exports = {
353353
if (needCache) {
354354
this.cacheVm(raw, vm)
355355
}
356+
// sync back changes for $value, particularly for
357+
// two-way bindings of primitive values
358+
var self = this
359+
vm.$watch('$value', function (val) {
360+
if (self.converted) {
361+
self.rawValue[vm.$key] = val
362+
} else {
363+
self.rawValue.$set(vm.$index, val)
364+
}
365+
})
356366
return vm
357367
},
358368

@@ -537,6 +547,8 @@ function findNextVm (vm, ref) {
537547
*/
538548

539549
function objToArray (obj) {
550+
// regardless of type, store the un-filtered raw value.
551+
this.rawValue = obj
540552
if (!isPlainObject(obj)) {
541553
return obj
542554
}

test/unit/specs/directives/repeat_spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,27 @@ if (_.inBrowser) {
603603
}, 30)
604604
})
605605

606+
it('sync $value changes back to original array/object', function (done) {
607+
var vm = new Vue({
608+
el: el,
609+
template:
610+
'<div v-repeat="items">{{$value}}</div>' +
611+
'<div v-repeat="obj">{{$value}}</div>',
612+
data: {
613+
items: ['a', 'b'],
614+
obj: { foo: 'a', bar: 'b' }
615+
}
616+
})
617+
vm._children[0].$value = 'c'
618+
var key = vm._children[2].$key
619+
vm._children[2].$value = 'd'
620+
_.nextTick(function () {
621+
expect(vm.items[0]).toBe('c')
622+
expect(vm.obj[key]).toBe('d')
623+
done()
624+
})
625+
})
626+
606627
})
607628
}
608629

0 commit comments

Comments
 (0)