File tree Expand file tree Collapse file tree 2 files changed +48
-3
lines changed
test/unit/specs/directives/public/for Expand file tree Collapse file tree 2 files changed +48
-3
lines changed Original file line number Diff line number Diff line change 11import FragmentFactory from '../../fragment/factory'
22import { FOR } from '../priorities'
33import { withoutConversion } from '../../observer/index'
4+ import { getPath } from '../../parsers/path'
45import {
56 isObject ,
67 warn ,
@@ -405,7 +406,7 @@ const vFor = {
405406 id = trackByKey
406407 ? trackByKey === '$index'
407408 ? index
408- : value [ trackByKey ]
409+ : getPath ( value , trackByKey )
409410 : ( key || value )
410411 if ( ! cache [ id ] ) {
411412 cache [ id ] = frag
@@ -446,7 +447,7 @@ const vFor = {
446447 var id = trackByKey
447448 ? trackByKey === '$index'
448449 ? index
449- : value [ trackByKey ]
450+ : getPath ( value , trackByKey )
450451 : ( key || value )
451452 frag = this . cache [ id ]
452453 } else {
@@ -478,7 +479,7 @@ const vFor = {
478479 var id = trackByKey
479480 ? trackByKey === '$index'
480481 ? index
481- : value [ trackByKey ]
482+ : getPath ( value , trackByKey )
482483 : ( key || value )
483484 this . cache [ id ] = null
484485 } else {
Original file line number Diff line number Diff line change @@ -528,6 +528,50 @@ describe('v-for', function () {
528528 }
529529 } )
530530
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+
531575 it ( 'track by $index' , function ( done ) {
532576 var vm = new Vue ( {
533577 el : el ,
You can’t perform that action at this time.
0 commit comments