Skip to content

Commit 0031c60

Browse files
committed
handle v-repeat switching between object & array (fix vuejs#869)
1 parent a316dfe commit 0031c60

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/directives/repeat.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,9 @@ module.exports = {
522522
uncacheVm: function (vm) {
523523
var data = vm._raw
524524
var idKey = this.idKey
525-
if (idKey || this.converted) {
526-
var id = idKey ? data[idKey] : vm.$key
525+
var convertedKey = vm.$key
526+
if (idKey || convertedKey) {
527+
var id = idKey ? data[idKey] : convertedKey
527528
this.cache[id] = null
528529
} else if (isObject(data)) {
529530
data[this.id] = null
@@ -573,6 +574,7 @@ function objToArray (obj) {
573574
// regardless of type, store the un-filtered raw value.
574575
this.rawValue = obj
575576
if (!isPlainObject(obj)) {
577+
this.converted = false
576578
return obj
577579
}
578580
var keys = Object.keys(obj)

test/unit/specs/directives/repeat_spec.js

+26
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,32 @@ if (_.inBrowser) {
670670
}
671671
})
672672

673+
it('switch between object-converted & array mode', function (done) {
674+
var obj = {
675+
a: { msg: 'AA' },
676+
b: { msg: 'BB' }
677+
}
678+
var arr = [obj.b, obj.a]
679+
var vm = new Vue({
680+
el: el,
681+
template: '<div v-repeat="obj">{{msg}}</div>',
682+
data: {
683+
obj: obj
684+
}
685+
})
686+
expect(el.innerHTML).toBe(Object.keys(obj).map(function (key) {
687+
return '<div>' + obj[key].msg + '</div>'
688+
}).join(''))
689+
vm.obj = arr
690+
_.nextTick(function () {
691+
expect(el.innerHTML).toBe('<div>BB</div><div>AA</div>')
692+
// make sure it cleared the cache
693+
expect(vm._directives[0].cache.a).toBeNull()
694+
expect(vm._directives[0].cache.b).toBeNull()
695+
done()
696+
})
697+
})
698+
673699
})
674700
}
675701

0 commit comments

Comments
 (0)