Skip to content

Commit 84e25a1

Browse files
stalniymanucorporat
authored andcommitted
feat(infinite-scroll): add waitFor method to InfiniteScroll
This allows to use `$event.waitFor(request())` inside template within `infinite` event. So that, user components do not depend on InifiniteScroll inside javascript.
1 parent 30980b6 commit 84e25a1

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

src/components/infinite-scroll/infinite-scroll.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,52 @@ import { DomController } from '../../platform/dom-controller';
5757
* }
5858
* ```
5959
*
60+
* ## `waitFor` method of InfiniteScroll
61+
*
62+
* In case if your async operation returns promise you can utilize
63+
* `waitFor` method inside your template.
64+
*
65+
* ```html
66+
* <ion-content>
67+
*
68+
* <ion-list>
69+
* <ion-item *ngFor="let item of items">{{item}}</ion-item>
70+
* </ion-list>
71+
*
72+
* <ion-infinite-scroll (ionInfinite)="$event.waitFor(doInfinite())">
73+
* <ion-infinite-scroll-content></ion-infinite-scroll-content>
74+
* </ion-infinite-scroll>
75+
*
76+
* </ion-content>
77+
* ```
78+
*
79+
* ```ts
80+
* @Component({...})
81+
* export class NewsFeedPage {
82+
* items = [];
83+
*
84+
* constructor() {
85+
* for (var i = 0; i < 30; i++) {
86+
* this.items.push( this.items.length );
87+
* }
88+
* }
89+
*
90+
* doInfinite(): Promise<any> {
91+
* console.log('Begin async operation');
92+
*
93+
* return new Promise((resolve) => {
94+
* setTimeout(() => {
95+
* for (var i = 0; i < 30; i++) {
96+
* this.items.push( this.items.length );
97+
* }
98+
*
99+
* console.log('Async operation has ended');
100+
* resolve();
101+
* }, 500);
102+
* })
103+
* }
104+
* }
105+
* ```
60106
*
61107
* ## Infinite Scroll Content
62108
*
@@ -221,7 +267,18 @@ export class InfiniteScroll {
221267
* to `enabled`.
222268
*/
223269
complete() {
224-
this.state = STATE_ENABLED;
270+
if (this.state === STATE_LOADING) {
271+
this.state = STATE_ENABLED;
272+
}
273+
}
274+
275+
/**
276+
* Pass a promise inside `waitFor()` within the `infinite` output event handler in order to
277+
* change state of infiniteScroll to "complete"
278+
*/
279+
waitFor(action: Promise) {
280+
const enable = this.complete.bind(this);
281+
action.then(enable, enable);
225282
}
226283

227284
/**

src/components/infinite-scroll/test/basic/app.module.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@ export class E2EPage1 {
1616
}
1717
}
1818

19-
doInfinite(infiniteScroll: InfiniteScroll) {
19+
doInfinite(): Promise<any> {
2020
console.log('Begin async operation');
2121

22-
getAsyncData().then(newData => {
22+
return getAsyncData().then(newData => {
2323
for (var i = 0; i < newData.length; i++) {
2424
this.items.push( this.items.length );
2525
}
2626

2727
console.log('Finished receiving data, async operation complete');
28-
infiniteScroll.complete();
2928

3029
if (this.items.length > 90) {
3130
this.enabled = false;

src/components/infinite-scroll/test/basic/main.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</button>
2424
</ion-list>
2525

26-
<ion-infinite-scroll (ionInfinite)="doInfinite($event)" [enabled]="enabled" threshold="100px">
26+
<ion-infinite-scroll (ionInfinite)="$event.waitFor(doInfinite())" [enabled]="enabled" threshold="100px">
2727
<ion-infinite-scroll-content
2828
loadingSpinner="bubbles"
2929
loadingText="Loading more data...">

0 commit comments

Comments
 (0)