Skip to content

Commit b7f658d

Browse files
committed
Handle for changing all props.
1 parent 4f1921a commit b7f658d

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

index.js

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,15 @@
5252

5353
created: function () {
5454
var start = this.start >= this.remain ? this.start : 0
55-
var height = this.size * this.remain
56-
var bench = this.bench || this.remain
57-
var keeps = this.remain + bench
55+
var keeps = this.remain + (this.bench || this.remain)
5856

5957
this.delta = {
6058
start: start, // start index.
6159
end: start + keeps, // end index.
6260
keeps: keeps, // nums keeping in real dom.
63-
bench: bench, // nums scroll pass should force update.
6461
total: 0, // all items count, update in render filter.
6562
offset: 0, // cache scrollTop offset.
6663
direct: 'd', // cache scroll direction.
67-
height: height, // container wrapper viewport height.
6864
fireTime: 0, // cache last event fire time avoid compact.
6965
paddingTop: 0, // container wrapper real padding-top.
7066
paddingBottom: 0, // container wrapper real padding-bottom.
@@ -82,19 +78,14 @@
8278
},
8379

8480
watch: {
85-
start: function (index) {
86-
var delta = this.delta
87-
var zone = this.getZone(index)
88-
89-
var scrollTop = this.variable
90-
? this.getVarOffset(zone.overflow ? delta.total : zone.start)
91-
: zone.overflow ? delta.total * this.size : zone.start * this.size
92-
93-
delta.end = zone.end
94-
delta.start = zone.start >= this.remain ? zone.start : 0
95-
96-
this.$forceUpdate()
97-
Vue2.nextTick(this.setScrollTop.bind(this, scrollTop))
81+
start: function () {
82+
this.alter = 'start'
83+
},
84+
remain: function () {
85+
this.alter = 'remain'
86+
},
87+
bench: function () {
88+
this.alter = 'bench'
9889
}
9990
},
10091

@@ -134,9 +125,10 @@
134125

135126
var delta = this.delta
136127
var zone = this.getZone(overs)
128+
var bench = this.bench || this.remain
137129

138130
// for better performance, if scroll pass items within now bench, do not update.
139-
if (!zone.overflow && (overs > delta.start) && (overs - delta.start <= delta.bench)) {
131+
if (!zone.overflow && (overs > delta.start) && (overs - delta.start <= bench)) {
140132
return
141133
}
142134

@@ -234,7 +226,7 @@
234226
// avoid overflow range.
235227
isOverflow: function (start) {
236228
var delta = this.delta
237-
var overflow = (delta.total > delta.keeps) && (start + this.remain >= delta.total)
229+
var overflow = (delta.total > delta.keeps && start + this.remain >= delta.total) || (start >= delta.total)
238230
if (overflow && delta.direct === 'd') {
239231
this.triggerEvent('tobottom')
240232
}
@@ -263,7 +255,7 @@
263255
// if overflow range return the last zone.
264256
if (overflow) {
265257
end = delta.total
266-
start = delta.total - delta.keeps
258+
start = Math.max(0, delta.total - delta.keeps)
267259
} else {
268260
start = index
269261
end = start + delta.keeps
@@ -313,6 +305,32 @@
313305
}
314306
},
315307

308+
// update delta and zone when prorps change.
309+
beforeUpdate: function () {
310+
var delta = this.delta
311+
delta.keeps = this.remain + (this.bench || this.remain)
312+
313+
var alterStart = this.alter === 'start'
314+
var oldStart = alterStart ? this.start : delta.start
315+
var zone = this.getZone(oldStart)
316+
317+
// if changing start, update scroll position after update.
318+
if (alterStart) {
319+
this.$nextTick(this.setScrollTop.bind(this, this.variable
320+
? this.getVarOffset(zone.overflow ? delta.total : zone.start)
321+
: zone.overflow ? delta.total * this.size : zone.start * this.size)
322+
)
323+
}
324+
325+
// if points out difference, force update once again.
326+
if (oldStart !== zone.start || this.alter) {
327+
this.alter = ''
328+
delta.end = zone.end
329+
delta.start = zone.start
330+
this.$forceUpdate()
331+
}
332+
},
333+
316334
render: function (h) {
317335
var list = this.filter()
318336
var delta = this.delta
@@ -323,7 +341,7 @@
323341
'style': {
324342
'display': 'block',
325343
'overflow-y': 'auto',
326-
'height': delta.height + 'px'
344+
'height': this.size * this.remain + 'px'
327345
},
328346
'on': {
329347
'scroll': dbc ? _debounce(this.onScroll.bind(this), dbc) : this.onScroll

0 commit comments

Comments
 (0)