Skip to content

Commit 04cf141

Browse files
committed
better vm caching for object repeats
1 parent 0926c29 commit 04cf141

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/directives/repeat.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,20 @@ module.exports = {
270270
for (i = 0, l = data.length; i < l; i++) {
271271
obj = data[i]
272272
raw = converted ? obj.$value : obj
273-
vm = !init && this.getVm(raw)
273+
vm = !init && this.getVm(raw, converted ? obj.$key : null)
274274
if (vm) { // reusable instance
275275
vm._reused = true
276276
vm.$index = i // update $index
277-
if (converted) {
278-
vm.$key = obj.$key // update $key
279-
}
280-
if (idKey) { // swap track by id data
277+
// update data for track-by or object repeat,
278+
// since in these two cases the data is replaced
279+
// rather than mutated.
280+
if (idKey || converted) {
281281
if (alias) {
282282
vm[alias] = raw
283+
} else if (_.isPlainObject(raw)) {
284+
vm.$data = raw
283285
} else {
284-
vm._setData(raw)
286+
vm.$value = raw
285287
}
286288
}
287289
} else { // new instance
@@ -391,7 +393,7 @@ module.exports = {
391393
vm._repeat = true
392394
// cache instance
393395
if (needCache) {
394-
this.cacheVm(raw, vm)
396+
this.cacheVm(raw, vm, this.converted ? meta.$key : null)
395397
}
396398
// sync back changes for $value, particularly for
397399
// two-way bindings of primitive values
@@ -436,14 +438,15 @@ module.exports = {
436438
*
437439
* @param {Object} data
438440
* @param {Vue} vm
441+
* @param {String} [key]
439442
*/
440443

441-
cacheVm: function (data, vm) {
444+
cacheVm: function (data, vm, key) {
442445
var idKey = this.idKey
443446
var cache = this.cache
444447
var id
445-
if (idKey) {
446-
id = data[idKey]
448+
if (key || idKey) {
449+
id = idKey ? data[idKey] : key
447450
if (!cache[id]) {
448451
cache[id] = vm
449452
} else {
@@ -477,12 +480,15 @@ module.exports = {
477480
* Try to get a cached instance from a piece of data.
478481
*
479482
* @param {Object} data
483+
* @param {String} [key]
480484
* @return {Vue|undefined}
481485
*/
482486

483-
getVm: function (data) {
484-
if (this.idKey) {
485-
return this.cache[data[this.idKey]]
487+
getVm: function (data, key) {
488+
var idKey = this.idKey
489+
if (key || idKey) {
490+
var id = idKey ? data[idKey] : key
491+
return this.cache[id]
486492
} else if (isObject(data)) {
487493
return data[this.id]
488494
} else {
@@ -509,8 +515,10 @@ module.exports = {
509515

510516
uncacheVm: function (vm) {
511517
var data = vm._raw
512-
if (this.idKey) {
513-
this.cache[data[this.idKey]] = null
518+
var idKey = this.idKey
519+
if (idKey || this.converted) {
520+
var id = idKey ? data[idKey] : vm.$key
521+
this.cache[id] = null
514522
} else if (isObject(data)) {
515523
data[this.id] = null
516524
vm._raw = null

src/vue.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ Object.defineProperty(p, '$data', {
5858
return this._data
5959
},
6060
set: function (newData) {
61-
this._setData(newData)
61+
if (newData !== this._data) {
62+
this._setData(newData)
63+
}
6264
}
6365
})
6466

0 commit comments

Comments
 (0)