Skip to content

Commit 79f470c

Browse files
committed
Improve the logic of events to export more concise API
1 parent 4797a55 commit 79f470c

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

src/components/InfiniteLoading.vue

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<template>
22
<div class="infinite-loading-container">
33
<i :class="spinnerType" v-show="isLoading"></i>
4-
<div class="infinite-status-prompt" v-show="!isLoading && isNoResults">
4+
<div class="infinite-status-prompt" v-show="!isLoading && isComplete && isFirstLoad">
55
<slot name="no-results">No results :(</slot>
66
</div>
7-
<div class="infinite-status-prompt" v-show="!isLoading && isNoMore">
7+
<div class="infinite-status-prompt" v-show="!isLoading && isComplete && !isFirstLoad">
88
<slot name="no-more">No more data :)</slot>
99
</div>
1010
</div>
@@ -52,11 +52,11 @@
5252
export default {
5353
data() {
5454
return {
55-
isLoading: false,
5655
scrollParent: null,
5756
scrollHandler: null,
58-
isNoResults: false,
59-
isNoMore: false,
57+
isLoading: false,
58+
isComplete: false,
59+
isFirstLoad: true, // save the current loading whether it is the first loading
6060
};
6161
},
6262
computed: {
@@ -90,32 +90,25 @@
9090
this.scrollParent.addEventListener('scroll', this.scrollHandler);
9191
},
9292
events: {
93-
// Hide the loading icon when data was loaded
9493
'$InfiniteLoading:loaded': function loaded() {
9594
this.isLoading = false;
95+
this.isFirstLoad = false;
9696
},
97-
'$InfiniteLoading:noResults': function noResults() {
98-
this.isLoading = false;
99-
this.isNoMore = false;
100-
this.isNoResults = true;
101-
this.scrollParent.removeEventListener('scroll', this.scrollHandler);
102-
},
103-
'$InfiniteLoading:noMore': function noMore() {
97+
'$InfiniteLoading:complete': function complete() {
10498
this.isLoading = false;
105-
this.isNoResults = false;
106-
this.isNoMore = true;
99+
this.isComplete = true;
107100
this.scrollParent.removeEventListener('scroll', this.scrollHandler);
108101
},
109102
'$InfiniteLoading:reset': function reset() {
110103
this.isLoading = false;
111-
this.isNoMore = false;
112-
this.isNoResults = false;
104+
this.isComplete = false;
105+
this.isFirstLoad = true;
113106
this.scrollParent.addEventListener('scroll', this.scrollHandler);
114107
setTimeout(this.scrollHandler, 1);
115108
},
116109
},
117110
destroyed() {
118-
if (!this.isNoResults && !this.isNoMore) {
111+
if (!this.isComplete) {
119112
this.scrollParent.removeEventListener('scroll', this.scrollHandler);
120113
}
121114
},

0 commit comments

Comments
 (0)