@@ -244,22 +244,23 @@ module.exports = {
244
244
var alias = this . arg
245
245
var init = ! oldVms
246
246
var vms = new Array ( data . length )
247
- var obj , raw , vm , i , l
247
+ var obj , raw , vm , i , l , primitive
248
248
// First pass, go through the new Array and fill up
249
249
// the new vms array. If a piece of data has a cached
250
250
// instance for it, we reuse it. Otherwise build a new
251
251
// instance.
252
252
for ( i = 0 , l = data . length ; i < l ; i ++ ) {
253
253
obj = data [ i ]
254
254
raw = converted ? obj . $value : obj
255
+ primitive = ! isObject ( raw )
255
256
vm = ! init && this . getVm ( raw , i , converted ? obj . $key : null )
256
257
if ( vm ) { // reusable instance
257
258
vm . _reused = true
258
259
vm . $index = i // update $index
259
260
// update data for track-by or object repeat,
260
261
// since in these two cases the data is replaced
261
262
// rather than mutated.
262
- if ( idKey || converted ) {
263
+ if ( idKey || converted || primitive ) {
263
264
if ( alias ) {
264
265
vm [ alias ] = raw
265
266
} else if ( _ . isPlainObject ( raw ) ) {
@@ -440,19 +441,20 @@ module.exports = {
440
441
cacheVm : function ( data , vm , index , key ) {
441
442
var idKey = this . idKey
442
443
var cache = this . cache
444
+ var primitive = ! isObject ( data )
443
445
var id
444
- if ( key || idKey ) {
446
+ if ( key || idKey || primitive ) {
445
447
id = idKey
446
448
? idKey === '$index'
447
449
? index
448
450
: data [ idKey ]
449
- : key
451
+ : ( key || index )
450
452
if ( ! cache [ id ] ) {
451
453
cache [ id ] = vm
452
- } else {
454
+ } else if ( ! primitive && idKey !== '$index' ) {
453
455
_ . warn ( 'Duplicate track-by key in v-repeat: ' + id )
454
456
}
455
- } else if ( isObject ( data ) ) {
457
+ } else {
456
458
id = this . id
457
459
if ( data . hasOwnProperty ( id ) ) {
458
460
if ( data [ id ] === null ) {
@@ -466,12 +468,6 @@ module.exports = {
466
468
} else {
467
469
_ . define ( data , id , vm )
468
470
}
469
- } else {
470
- if ( ! cache [ data ] ) {
471
- cache [ data ] = [ vm ]
472
- } else {
473
- cache [ data ] . push ( vm )
474
- }
475
471
}
476
472
vm . _raw = data
477
473
} ,
@@ -487,28 +483,16 @@ module.exports = {
487
483
488
484
getVm : function ( data , index , key ) {
489
485
var idKey = this . idKey
490
- if ( key || idKey ) {
486
+ var primitive = ! isObject ( data )
487
+ if ( key || idKey || primitive ) {
491
488
var id = idKey
492
489
? idKey === '$index'
493
490
? index
494
491
: data [ idKey ]
495
- : key
492
+ : ( key || index )
496
493
return this . cache [ id ]
497
- } else if ( isObject ( data ) ) {
498
- return data [ this . id ]
499
494
} 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 ]
512
496
}
513
497
} ,
514
498
@@ -521,19 +505,19 @@ module.exports = {
521
505
uncacheVm : function ( vm ) {
522
506
var data = vm . _raw
523
507
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 ) {
526
512
var id = idKey
527
513
? idKey === '$index'
528
- ? vm . $ index
514
+ ? index
529
515
: data [ idKey ]
530
- : convertedKey
516
+ : ( key || index )
531
517
this . cache [ id ] = null
532
- } else if ( isObject ( data ) ) {
518
+ } else {
533
519
data [ this . id ] = null
534
520
vm . _raw = null
535
- } else {
536
- this . cache [ data ] . pop ( )
537
521
}
538
522
} ,
539
523
0 commit comments