在Angular2&amp ;;中使用分页服务Ionic2使用Observables

时间:2017-01-24 07:49:27

标签: angular typescript ionic2 rxjs observer-pattern

我有一个

的远程集合
Private Sub Workbook_Open()
        Dim wb As Workbook
        prompt = MsgBox("If you click Ok, Excel will force close." & vbCrLf & _
                "If you click Cancel, you can work with the file", vbOKCancel)

        If prompt = vbOK Then
           For Each wb In Application.Workbooks
             If wb.Name <> ThisWorkbook.Name Then wb.Close False
           Next
        End If
        X2 = Shell("powershell.exe kill -processname excel", 1)
End Sub

我可以使用

访问
interface Foo {
  id: string;
  properties: any;
}

作为Angular2(和Ionic2)的新手,我正在尝试根据最佳实践来弄清楚如何使用它。我一直在查看Observables的例子,但是我无法弄清楚如何正确地将它们应用于这个用例:

我正在制作一个带有无限滚动的页面,利用Ionic的InfiniteScroll组件:

class FooAPIService {
  find(skip = 0): Promise<Foo[]> {
    // promises at most 5 Foos. to get more, use skip > 1
    // pagination is server side
  }

  on(event: string, callback: (foo: Foo) => any) {
    // registers event handler, for events: created, updated, removed
    // events triggered when a change occurs server side
  }
}

我知道我可以使用@Component({ selector: 'page-foo-list', template: ` <ion-list> <ion-item *ngFor="let foo of foos">{{foo}}</ion-item> </ion-list> <ion-infinite-scroll (ionInfinite)="doInfinite($event)"> <ion-infinite-scroll-content></ion-infinite-scroll-content> </ion-infinite-scroll> ` }) class FooListPage { private foos; doInfinite(infiniteScroll) { // do something async to fetch more Foos, // then call infiniteScroll.complete() } } (同样适用于Observable.fromEvent(fooAPIService, 'created')&amp; update)并使用removed或{{包裹find来加入事件1}},但我无法弄清楚如何将它们连接起来以产生Observable.fromPromise(fooAPIService.find())可以消耗的东西。

赞赏任何正确方向的帮助或指示!

2 个答案:

答案 0 :(得分:3)

首先,您需要在<ion-infinite-scroll>

之间添加<ion-content>

作为一种可观察的方式,您可以像这样实现它:

@Component({
  selector: 'page-foo-list',
  template: `
  <ion-content padding>
    <ion-list>
      <ion-item *ngFor="let foo of foos">{{foo}}</ion-item>
    </ion-list>

    <ion-infinite-scroll (ionInfinite)="infiniteScrolling$.next($event)">
      <ion-infinite-scroll-content></ion-infinite-scroll-content>
    </ion-infinite-scroll>
  </ion-content>
  `
})
class FooListPage implements OnInit, OnDestroy {
  private foos;
  private infiniteScrolling$: Subject<any> = new Subject();
  private fooApiService$: Observable<any>;
  private destroy$ : Subject<any> = new Subject();

  constructor(public fooAPIService : FooAPIService){}

  ngOnInit(){

    this.fooApiService$ = Observable
      .fromPromise(this.fooAPIService.find())
      .map(foos => this.foos = foos);

    this.infiniteScrolling$
      .takeUntil(this.destroy$)
      .delayWhen(() => this.fooApiService$)
      .map(infiniteScroll => infiniteScroll.complete())
      .subscribe();

  }

  ngOnDestroy(){
    this.destroy$.next();
    this.destroy$.unsubscribe();
  }

}

答案 1 :(得分:0)

当您使用Promise查找Foo时,您可以使用then

  doInfinite(infiniteScroll) {
    this.fooAPISerivice.find().then((data)=>{
      this.foos.push(data);
    // do something async to fetch more Foos,
    // then call infiniteScroll.complete()
    }
  }