Skip to content

Commit 6ac14be

Browse files
committed
support paths for track-by (close #2632)
1 parent 79442b3 commit 6ac14be

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/directives/public/for.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import FragmentFactory from '../../fragment/factory'
22
import { FOR } from '../priorities'
33
import { withoutConversion } from '../../observer/index'
4+
import { getPath } from '../../parsers/path'
45
import {
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 {

test/unit/specs/directives/public/for/for_spec.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff 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,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy