handle v-repeat switching between object & array (fix #869)

This commit is contained in:
Evan You 2015-05-27 15:56:49 -04:00
parent a316dfe9a4
commit 0031c60262
2 changed files with 30 additions and 2 deletions

View File

@ -522,8 +522,9 @@ module.exports = {
uncacheVm: function (vm) {
var data = vm._raw
var idKey = this.idKey
if (idKey || this.converted) {
var id = idKey ? data[idKey] : vm.$key
var convertedKey = vm.$key
if (idKey || convertedKey) {
var id = idKey ? data[idKey] : convertedKey
this.cache[id] = null
} else if (isObject(data)) {
data[this.id] = null
@ -573,6 +574,7 @@ function objToArray (obj) {
// regardless of type, store the un-filtered raw value.
this.rawValue = obj
if (!isPlainObject(obj)) {
this.converted = false
return obj
}
var keys = Object.keys(obj)

View File

@ -670,6 +670,32 @@ if (_.inBrowser) {
}
})
it('switch between object-converted & array mode', function (done) {
var obj = {
a: { msg: 'AA' },
b: { msg: 'BB' }
}
var arr = [obj.b, obj.a]
var vm = new Vue({
el: el,
template: '<div v-repeat="obj">{{msg}}</div>',
data: {
obj: obj
}
})
expect(el.innerHTML).toBe(Object.keys(obj).map(function (key) {
return '<div>' + obj[key].msg + '</div>'
}).join(''))
vm.obj = arr
_.nextTick(function () {
expect(el.innerHTML).toBe('<div>BB</div><div>AA</div>')
// make sure it cleared the cache
expect(vm._directives[0].cache.a).toBeNull()
expect(vm._directives[0].cache.b).toBeNull()
done()
})
})
})
}