Skip to content

Commit 9533bfb

Browse files
committed
Add the $state special argument for the callback to change component states #57
1 parent c224764 commit 9533bfb

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

src/components/InfiniteLoading.vue

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,30 @@
2323
spiral: 'loading-spiral',
2424
waveDots: 'loading-wave-dots',
2525
};
26+
const warnings = {
27+
stateChanger: [
28+
'[Vue-infinite-loading warn]: emit `loaded` and `complete` event through component instance of `$refs` may cause error, so it will be deprecated soon, please use the `$state` argument instead (`$state` just the special `$event` variable):',
29+
'\ntemplate:',
30+
'<infinite-loading @infinite="infiniteHandler"></infinite-loading>',
31+
`
32+
script:
33+
...
34+
infiniteHandler($state) {
35+
ajax('https://www.example.com/api/news')
36+
.then((res) => {
37+
if (res.data.length) {
38+
$state.loaded();
39+
} else {
40+
$state.complete();
41+
}
42+
});
43+
}
44+
...`,
45+
'',
46+
'more details: https://github.com/PeachScript/vue-infinite-loading/issues/57#issuecomment-324370549',
47+
].join('\n'),
48+
infiniteEvent: '[Vue-infinite-loading warn]: `:on-infinite` property will be deprecated soon, please use `@infinite` event instead.',
49+
};
2650
2751
export default {
2852
data() {
@@ -89,22 +113,32 @@
89113
setTimeout(this.scrollHandler, 1);
90114
this.scrollParent.addEventListener('scroll', this.scrollHandler);
91115
92-
this.$on('$InfiniteLoading:loaded', () => {
116+
this.$on('$InfiniteLoading:loaded', (ev) => {
93117
this.isFirstLoad = false;
118+
94119
if (this.isLoading) {
95120
this.$nextTick(this.attemptLoad);
96121
}
122+
123+
if (!ev || ev.target !== this) {
124+
console.warn(warnings.stateChanger);
125+
}
97126
});
98127
99-
this.$on('$InfiniteLoading:complete', () => {
128+
this.$on('$InfiniteLoading:complete', (ev) => {
100129
this.isLoading = false;
101130
this.isComplete = true;
102131
103132
// force re-complation computed properties to fix the problem of get slot text delay
104133
this.$nextTick(() => {
105134
this.$forceUpdate();
106135
});
136+
107137
this.scrollParent.removeEventListener('scroll', this.scrollHandler);
138+
139+
if (!ev || ev.target !== this) {
140+
console.warn(warnings.stateChanger);
141+
}
108142
});
109143
110144
this.$on('$InfiniteLoading:reset', () => {
@@ -116,8 +150,23 @@
116150
});
117151
118152
if (this.onInfinite) {
119-
console.warn('[Vue-infinite-loading warn]: `:on-infinite` property will be deprecated soon, please use `@infinite` event instead.');
153+
console.warn(warnings.infiniteEvent);
120154
}
155+
156+
/**
157+
* change state for this component, pass to the callback
158+
*/
159+
this.stateChanger = {
160+
loaded: () => {
161+
this.$emit('$InfiniteLoading:loaded', { target: this });
162+
},
163+
complete: () => {
164+
this.$emit('$InfiniteLoading:complete', { target: this });
165+
},
166+
reset: () => {
167+
this.$emit('$InfiniteLoading:reset', { target: this });
168+
},
169+
};
121170
},
122171
/**
123172
* To adapt to keep-alive feature, but only work on Vue 2.2.0 and above, see: https://vuejs.org/v2/api/#keep-alive
@@ -140,9 +189,9 @@
140189
this.isLoading = true;
141190
142191
if (typeof this.onInfinite === 'function') {
143-
this.onInfinite.call();
192+
this.onInfinite.call(null, this.stateChanger);
144193
} else {
145-
this.$emit('infinite');
194+
this.$emit('infinite', this.stateChanger);
146195
}
147196
} else {
148197
this.isLoading = false;

0 commit comments

Comments
 (0)