Skip to content

Commit 76d6b78

Browse files
committed
use track-by="$index" for primitive values
1 parent 44f7a6d commit 76d6b78

File tree

1 file changed

+19
-35
lines changed

1 file changed

+19
-35
lines changed

src/directives/repeat.js

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -244,22 +244,23 @@ module.exports = {
244244
var alias = this.arg
245245
var init = !oldVms
246246
var vms = new Array(data.length)
247-
var obj, raw, vm, i, l
247+
var obj, raw, vm, i, l, primitive
248248
// First pass, go through the new Array and fill up
249249
// the new vms array. If a piece of data has a cached
250250
// instance for it, we reuse it. Otherwise build a new
251251
// instance.
252252
for (i = 0, l = data.length; i < l; i++) {
253253
obj = data[i]
254254
raw = converted ? obj.$value : obj
255+
primitive = !isObject(raw)
255256
vm = !init && this.getVm(raw, i, converted ? obj.$key : null)
256257
if (vm) { // reusable instance
257258
vm._reused = true
258259
vm.$index = i // update $index
259260
// update data for track-by or object repeat,
260261
// since in these two cases the data is replaced
261262
// rather than mutated.
262-
if (idKey || converted) {
263+
if (idKey || converted || primitive) {
263264
if (alias) {
264265
vm[alias] = raw
265266
} else if (_.isPlainObject(raw)) {
@@ -440,19 +441,20 @@ module.exports = {
440441
cacheVm: function (data, vm, index, key) {
441442
var idKey = this.idKey
442443
var cache = this.cache
444+
var primitive = !isObject(data)
443445
var id
444-
if (key || idKey) {
446+
if (key || idKey || primitive) {
445447
id = idKey
446448
? idKey === '$index'
447449
? index
448450
: data[idKey]
449-
: key
451+
: (key || index)
450452
if (!cache[id]) {
451453
cache[id] = vm
452-
} else {
454+
} else if (!primitive && idKey !== '$index') {
453455
_.warn('Duplicate track-by key in v-repeat: ' + id)
454456
}
455-
} else if (isObject(data)) {
457+
} else {
456458
id = this.id
457459
if (data.hasOwnProperty(id)) {
458460
if (data[id] === null) {
@@ -466,12 +468,6 @@ module.exports = {
466468
} else {
467469
_.define(data, id, vm)
468470
}
469-
} else {
470-
if (!cache[data]) {
471-
cache[data] = [vm]
472-
} else {
473-
cache[data].push(vm)
474-
}
475471
}
476472
vm._raw = data
477473
},
@@ -487,28 +483,16 @@ module.exports = {
487483

488484
getVm: function (data, index, key) {
489485
var idKey = this.idKey
490-
if (key || idKey) {
486+
var primitive = !isObject(data)
487+
if (key || idKey || primitive) {
491488
var id = idKey
492489
? idKey === '$index'
493490
? index
494491
: data[idKey]
495-
: key
492+
: (key || index)
496493
return this.cache[id]
497-
} else if (isObject(data)) {
498-
return data[this.id]
499494
} else {
500-
var cached = this.cache[data]
501-
if (cached) {
502-
var i = 0
503-
var vm = cached[i]
504-
// since duplicated vm instances might be a reused
505-
// one OR a newly created one, we need to return the
506-
// first instance that is neither of these.
507-
while (vm && (vm._reused || vm._new)) {
508-
vm = cached[++i]
509-
}
510-
return vm
511-
}
495+
return data[this.id]
512496
}
513497
},
514498

@@ -521,19 +505,19 @@ module.exports = {
521505
uncacheVm: function (vm) {
522506
var data = vm._raw
523507
var idKey = this.idKey
524-
var convertedKey = vm.$key
525-
if (idKey || convertedKey) {
508+
var index = vm.$index
509+
var key = vm.$key
510+
var primitive = !isObject(data)
511+
if (idKey || key || primitive) {
526512
var id = idKey
527513
? idKey === '$index'
528-
? vm.$index
514+
? index
529515
: data[idKey]
530-
: convertedKey
516+
: (key || index)
531517
this.cache[id] = null
532-
} else if (isObject(data)) {
518+
} else {
533519
data[this.id] = null
534520
vm._raw = null
535-
} else {
536-
this.cache[data].pop()
537521
}
538522
},
539523

0 commit comments

Comments
 (0)