Skip to content

Commit

Permalink
Step 13.5: Re-subscribe on current page changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela authored and DAB0mB committed Dec 14, 2016
1 parent ee439ba commit ed14458
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions client/imports/app/parties/parties-list.component.ts
Expand Up @@ -4,6 +4,8 @@ import { Subject } from 'rxjs/Subject';
import { Subscription } from 'rxjs/Subscription';
import { MeteorObservable } from 'meteor-rxjs';

import 'rxjs/add/operator/combineLatest';

import { Parties } from '../../../../both/collections/parties.collection';
import { Party } from '../../../../both/models/party.model';

Expand All @@ -28,20 +30,31 @@ export class PartiesListComponent implements OnInit, OnDestroy {
pageSize: Subject<number> = new Subject<number>();
curPage: Subject<number> = new Subject<number>();
nameOrder: Subject<number> = new Subject<number>();
optionsSub: Subscription;

ngOnInit() {
const options: Options = {
limit: this.pageSize,
skip: (this.curPage - 1) * this.pageSize,
sort: { name: this.nameOrder }
};

this.partiesSub = MeteorObservable.subscribe('parties', options).subscribe(() => {
this.parties = Parties.find({}, {
sort: {
name: this.nameOrder
}
}).zone();
this.optionsSub = Observable.combineLatest(
this.pageSize,
this.curPage,
this.nameOrder
).subscribe(([pageSize, curPage, nameOrder]) => {
const options: Options = {
limit: pageSize as number,
skip: ((curPage as number) - 1) * (pageSize as number),
sort: { name: nameOrder as number }
};

if (this.partiesSub) {
this.partiesSub.unsubscribe();
}

this.partiesSub = MeteorObservable.subscribe('parties', options).subscribe(() => {
this.parties = Parties.find({}, {
sort: {
name: nameOrder
}
}).zone();
});
});

this.pageSize.next(10);
Expand All @@ -59,5 +72,6 @@ export class PartiesListComponent implements OnInit, OnDestroy {

ngOnDestroy() {
this.partiesSub.unsubscribe();
this.optionsSub.unsubscribe();
}
}

0 comments on commit ed14458

Please sign in to comment.