Skip to content

Commit

Permalink
feat(infinite-scroll): add waitFor method to InfiniteScroll
Browse files Browse the repository at this point in the history
This allows to use `$event.waitFor(request())` inside template within `infinite` event. So that, user components do not depend on InifiniteScroll inside javascript.
  • Loading branch information
stalniy authored and manucorporat committed Mar 8, 2017
1 parent 30980b6 commit 84e25a1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
59 changes: 58 additions & 1 deletion src/components/infinite-scroll/infinite-scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,52 @@ import { DomController } from '../../platform/dom-controller';
* }
* ```
*
* ## `waitFor` method of InfiniteScroll
*
* In case if your async operation returns promise you can utilize
* `waitFor` method inside your template.
*
* ```html
* <ion-content>
*
* <ion-list>
* <ion-item *ngFor="let item of items">{{item}}</ion-item>
* </ion-list>
*
* <ion-infinite-scroll (ionInfinite)="$event.waitFor(doInfinite())">
* <ion-infinite-scroll-content></ion-infinite-scroll-content>
* </ion-infinite-scroll>
*
* </ion-content>
* ```
*
* ```ts
* @Component({...})
* export class NewsFeedPage {
* items = [];
*
* constructor() {
* for (var i = 0; i < 30; i++) {
* this.items.push( this.items.length );
* }
* }
*
* doInfinite(): Promise<any> {
* console.log('Begin async operation');
*
* return new Promise((resolve) => {
* setTimeout(() => {
* for (var i = 0; i < 30; i++) {
* this.items.push( this.items.length );
* }
*
* console.log('Async operation has ended');
* resolve();
* }, 500);
* })
* }
* }
* ```
*
* ## Infinite Scroll Content
*
Expand Down Expand Up @@ -221,7 +267,18 @@ export class InfiniteScroll {
* to `enabled`.
*/
complete() {
this.state = STATE_ENABLED;
if (this.state === STATE_LOADING) {
this.state = STATE_ENABLED;
}
}

/**
* Pass a promise inside `waitFor()` within the `infinite` output event handler in order to
* change state of infiniteScroll to "complete"
*/
waitFor(action: Promise) {
const enable = this.complete.bind(this);
action.then(enable, enable);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/components/infinite-scroll/test/basic/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ export class E2EPage1 {
}
}

doInfinite(infiniteScroll: InfiniteScroll) {
doInfinite(): Promise<any> {
console.log('Begin async operation');

getAsyncData().then(newData => {
return getAsyncData().then(newData => {
for (var i = 0; i < newData.length; i++) {
this.items.push( this.items.length );
}

console.log('Finished receiving data, async operation complete');
infiniteScroll.complete();

if (this.items.length > 90) {
this.enabled = false;
Expand Down
2 changes: 1 addition & 1 deletion src/components/infinite-scroll/test/basic/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</button>
</ion-list>

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

0 comments on commit 84e25a1

Please sign in to comment.