Skip to content

Commit 635a6e7

Browse files
committed
use more conservative check for v-repeat inplace update
1 parent 1a36e39 commit 635a6e7

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

src/directives/repeat.js

+27-12
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ module.exports = {
4848
this.idKey =
4949
this._checkParam('track-by') ||
5050
this._checkParam('trackby') // 0.11.0 compat
51-
this.hasTransition =
52-
this.el.hasAttribute(config.prefix + 'transition')
5351
this.cache = Object.create(null)
52+
this.checkUpdateStrategy()
5453
},
5554

5655
/**
@@ -131,6 +130,30 @@ module.exports = {
131130
}
132131
},
133132

133+
/**
134+
* Check what strategy to use for updates.
135+
*
136+
* If the repeat is simple enough we can use in-place
137+
* updates which simply overwrites existing instances'
138+
* data. This strategy reuses DOM nodes and instances
139+
* as much as possible.
140+
*
141+
* There are two situations where we have to use the
142+
* more complex but more accurate diff algorithm:
143+
* 1. We are using components with or inside v-repeat.
144+
* The components could have private state that needs
145+
* to be preserved across updates.
146+
* 2. We have transitions on the list, which requires
147+
* precise DOM re-positioning.
148+
*/
149+
150+
checkUpdateStrategy: function () {
151+
this.needDiff =
152+
this.asComponent ||
153+
this.el.hasAttribute(config.prefix + 'transition') ||
154+
this.template.querySelector('[' + config.prefix + 'component]')
155+
},
156+
134157
/**
135158
* Update.
136159
* This is called whenever the Array mutates.
@@ -146,14 +169,7 @@ module.exports = {
146169
} else if (type === 'string') {
147170
data = _.toArray(data)
148171
}
149-
// There are two situations where we have to use the
150-
// more complex but more accurate diff algorithm:
151-
// 1. We are using components with v-repeat - the
152-
// components could have additional state outside
153-
// of v-repeat data.
154-
// 2. We have transitions on the list, which requires
155-
// precise DOM re-positioning.
156-
this.vms = this.asComponent || this.hasTransition
172+
this.vms = this.needDiff
157173
? this.diff(data, this.vms)
158174
: this.inplaceUpdate(data, this.vms)
159175
// update v-ref
@@ -404,13 +420,12 @@ module.exports = {
404420
if (this.refID) {
405421
this.vm.$[this.refID] = null
406422
}
407-
var needUncache = this.asComponent || this.hasTransition
408423
if (this.vms) {
409424
var i = this.vms.length
410425
var vm
411426
while (i--) {
412427
vm = this.vms[i]
413-
if (needUncache) {
428+
if (this.needDiff) {
414429
this.uncacheVm(vm)
415430
}
416431
vm.$destroy()

0 commit comments

Comments
 (0)