Skip to content

Commit 676669c

Browse files
committed
Fix nested repeats with track-by.
When the parent vm is reused, newly created children will inherit the _reused property, and they won't be inserted into the dom. Fixes vuejs#782
1 parent b61088e commit 676669c

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/directives/repeat.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ module.exports = {
252252
} else { // new instance
253253
vm = this.build(obj, i, true)
254254
vm._new = true
255+
vm._reused = false
255256
}
256257
vms[i] = vm
257258
// insert if this is first run

test/unit/specs/directives/repeat_spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,60 @@ if (_.inBrowser) {
622622
})
623623
})
624624

625+
it('nested track by', function (done) {
626+
assertTrackBy('<div v-repeat="list" track-by="id">{{msg}}<div v-repeat="list" track-by="id">{{msg}}</div></div>', function () {
627+
assertTrackBy('<div v-transition v-repeat="list" track-by="id">{{msg}}<div v-transition v-repeat="list" track-by="id">{{msg}}</div></div>', done)
628+
})
629+
630+
function assertTrackBy(template, next) {
631+
var vm = new Vue({
632+
el: el,
633+
data: {
634+
list: [
635+
{ id: 1, msg: 'hi', list: [
636+
{ id: 1, msg: 'hi foo' }
637+
] },
638+
{ id: 2, msg: 'ha', list: [] },
639+
{ id: 3, msg: 'ho', list: [] }
640+
]
641+
},
642+
template: template
643+
})
644+
assertMarkup()
645+
646+
var oldVms = vm._children.slice()
647+
648+
vm.list = [
649+
{ id: 1, msg: 'wa', list: [
650+
{ id: 1, msg: 'hi foo' },
651+
{ id: 2, msg: 'hi bar' }
652+
] },
653+
{ id: 2, msg: 'wo', list: [] }
654+
]
655+
656+
_.nextTick(function () {
657+
assertMarkup()
658+
// should reuse old vms!
659+
var i = 2
660+
while (i--) {
661+
expect(vm._children[i]).toBe(oldVms[i])
662+
}
663+
expect(vm._children[0]._children[0]).toBe(oldVms[0]._children[0])
664+
next()
665+
})
666+
667+
function assertMarkup () {
668+
var markup = vm.list.map(function (item) {
669+
var sublist = item.list.map(function (item) {
670+
return '<div>' + item.msg + '</div>'
671+
}).join('') + '<!--v-repeat-->'
672+
return '<div>' + item.msg + sublist + '</div>'
673+
}).join('') + '<!--v-repeat-->'
674+
expect(el.innerHTML).toBe(markup)
675+
}
676+
}
677+
})
678+
625679
})
626680
}
627681

0 commit comments

Comments
 (0)