From 0031c60262e925392d8c4bdfb729dd77a7846c93 Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 27 May 2015 15:56:49 -0400 Subject: [PATCH] handle v-repeat switching between object & array (fix #869) --- src/directives/repeat.js | 6 ++++-- test/unit/specs/directives/repeat_spec.js | 26 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/directives/repeat.js b/src/directives/repeat.js index 8e8fe0f6..dce1a86d 100644 --- a/src/directives/repeat.js +++ b/src/directives/repeat.js @@ -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) diff --git a/test/unit/specs/directives/repeat_spec.js b/test/unit/specs/directives/repeat_spec.js index 738cbf8c..92236e0b 100644 --- a/test/unit/specs/directives/repeat_spec.js +++ b/test/unit/specs/directives/repeat_spec.js @@ -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: '
{{msg}}
', + data: { + obj: obj + } + }) + expect(el.innerHTML).toBe(Object.keys(obj).map(function (key) { + return '
' + obj[key].msg + '
' + }).join('')) + vm.obj = arr + _.nextTick(function () { + expect(el.innerHTML).toBe('
BB
AA
') + // make sure it cleared the cache + expect(vm._directives[0].cache.a).toBeNull() + expect(vm._directives[0].cache.b).toBeNull() + done() + }) + }) + }) }