File tree 2 files changed +48
-3
lines changed
test/unit/specs/directives/public/for
2 files changed +48
-3
lines changed Original file line number Diff line number Diff line change 1
1
import FragmentFactory from '../../fragment/factory'
2
2
import { FOR } from '../priorities'
3
3
import { withoutConversion } from '../../observer/index'
4
+ import { getPath } from '../../parsers/path'
4
5
import {
5
6
isObject ,
6
7
warn ,
@@ -405,7 +406,7 @@ const vFor = {
405
406
id = trackByKey
406
407
? trackByKey === '$index'
407
408
? index
408
- : value [ trackByKey ]
409
+ : getPath ( value , trackByKey )
409
410
: ( key || value )
410
411
if ( ! cache [ id ] ) {
411
412
cache [ id ] = frag
@@ -446,7 +447,7 @@ const vFor = {
446
447
var id = trackByKey
447
448
? trackByKey === '$index'
448
449
? index
449
- : value [ trackByKey ]
450
+ : getPath ( value , trackByKey )
450
451
: ( key || value )
451
452
frag = this . cache [ id ]
452
453
} else {
@@ -478,7 +479,7 @@ const vFor = {
478
479
var id = trackByKey
479
480
? trackByKey === '$index'
480
481
? index
481
- : value [ trackByKey ]
482
+ : getPath ( value , trackByKey )
482
483
: ( key || value )
483
484
this . cache [ id ] = null
484
485
} else {
Original file line number Diff line number Diff line change @@ -528,6 +528,50 @@ describe('v-for', function () {
528
528
}
529
529
} )
530
530
531
+ it ( 'track by nested id path' , function ( done ) {
532
+ var vm = new Vue ( {
533
+ el : el ,
534
+ template : '<test v-for="item in list" :item="item" track-by="nested.id"></test>' ,
535
+ data : {
536
+ list : [
537
+ { nested : { id : 1 } , msg : 'foo' } ,
538
+ { nested : { id : 2 } , msg : 'bar' } ,
539
+ { nested : { id : 3 } , msg : 'baz' }
540
+ ]
541
+ } ,
542
+ components : {
543
+ test : {
544
+ props : [ 'item' ] ,
545
+ template : '{{item.msg}}'
546
+ }
547
+ }
548
+ } )
549
+ assertMarkup ( )
550
+ var oldVms = vm . $children . slice ( )
551
+ // swap the data with different objects, but with
552
+ // the same ID!
553
+ vm . list = [
554
+ { nested : { id : 1 } , msg : 'qux' } ,
555
+ { nested : { id : 2 } , msg : 'quux' }
556
+ ]
557
+ _ . nextTick ( function ( ) {
558
+ assertMarkup ( )
559
+ // should reuse old vms!
560
+ var i = 2
561
+ while ( i -- ) {
562
+ expect ( vm . $children [ i ] ) . toBe ( oldVms [ i ] )
563
+ }
564
+ done ( )
565
+ } )
566
+
567
+ function assertMarkup ( ) {
568
+ var markup = vm . list . map ( function ( item ) {
569
+ return '<test>' + item . msg + '</test>'
570
+ } ) . join ( '' )
571
+ expect ( el . innerHTML ) . toBe ( markup )
572
+ }
573
+ } )
574
+
531
575
it ( 'track by $index' , function ( done ) {
532
576
var vm = new Vue ( {
533
577
el : el ,
You can’t perform that action at this time.
0 commit comments