|
23 | 23 | spiral: 'loading-spiral',
|
24 | 24 | waveDots: 'loading-wave-dots',
|
25 | 25 | };
|
| 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 | + }; |
26 | 50 |
|
27 | 51 | export default {
|
28 | 52 | data() {
|
|
89 | 113 | setTimeout(this.scrollHandler, 1);
|
90 | 114 | this.scrollParent.addEventListener('scroll', this.scrollHandler);
|
91 | 115 |
|
92 |
| - this.$on('$InfiniteLoading:loaded', () => { |
| 116 | + this.$on('$InfiniteLoading:loaded', (ev) => { |
93 | 117 | this.isFirstLoad = false;
|
| 118 | +
|
94 | 119 | if (this.isLoading) {
|
95 | 120 | this.$nextTick(this.attemptLoad);
|
96 | 121 | }
|
| 122 | +
|
| 123 | + if (!ev || ev.target !== this) { |
| 124 | + console.warn(warnings.stateChanger); |
| 125 | + } |
97 | 126 | });
|
98 | 127 |
|
99 |
| - this.$on('$InfiniteLoading:complete', () => { |
| 128 | + this.$on('$InfiniteLoading:complete', (ev) => { |
100 | 129 | this.isLoading = false;
|
101 | 130 | this.isComplete = true;
|
102 | 131 |
|
103 | 132 | // force re-complation computed properties to fix the problem of get slot text delay
|
104 | 133 | this.$nextTick(() => {
|
105 | 134 | this.$forceUpdate();
|
106 | 135 | });
|
| 136 | +
|
107 | 137 | this.scrollParent.removeEventListener('scroll', this.scrollHandler);
|
| 138 | +
|
| 139 | + if (!ev || ev.target !== this) { |
| 140 | + console.warn(warnings.stateChanger); |
| 141 | + } |
108 | 142 | });
|
109 | 143 |
|
110 | 144 | this.$on('$InfiniteLoading:reset', () => {
|
|
116 | 150 | });
|
117 | 151 |
|
118 | 152 | 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); |
120 | 154 | }
|
| 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 | + }; |
121 | 170 | },
|
122 | 171 | /**
|
123 | 172 | * 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 | 189 | this.isLoading = true;
|
141 | 190 |
|
142 | 191 | if (typeof this.onInfinite === 'function') {
|
143 |
| - this.onInfinite.call(); |
| 192 | + this.onInfinite.call(null, this.stateChanger); |
144 | 193 | } else {
|
145 |
| - this.$emit('infinite'); |
| 194 | + this.$emit('infinite', this.stateChanger); |
146 | 195 | }
|
147 | 196 | } else {
|
148 | 197 | this.isLoading = false;
|
|
0 commit comments