Skip to content

Instantly share code, notes, and snippets.

@sonicparke
Last active December 6, 2016 02:16
Show Gist options
  • Save sonicparke/6931ff00628f6dfb5519c8d4111dc2ec to your computer and use it in GitHub Desktop.
Save sonicparke/6931ff00628f6dfb5519c8d4111dc2ec to your computer and use it in GitHub Desktop.
import { Component, OnInit, OnDestroy } from '@angular/core';
import { SubscriptionManager } from 'path/to/subscription_manager';
@Component({
selector: 'app-thing',
templateUrl: './app-thing.component.html',
styleUrls: ['./app-thing.component.scss']
})
export class PatientReviewComponent implements OnInit, OnDestroy {
public isLoading: boolean = true;
private Id: number;
private subscriptions: SubscriptionManager = new SubscriptionManager();
constructor(private service: Service) { }
private loadData(): void {
let sub = this.service.getQuestions(this.Id)
.subscribe(
(res: any) => {
this.data = res;
});
this.subscriptions.add(sub);
}
ngOnInit() {
this.loadData();
}
ngOnDestroy() {
this.subscriptions.destroy();
}
}
public getQuestions(Id: number): Observable<any[]> {
let url = '/endpoint';
let params: URLSearchParams = new URLSearchParams();
params.set('api_key', this.config.api_key);
let options: RequestOptions = new RequestOptions({search: params});
return this.api
.get(url, options)
.map(res => this.mapQuestionData(res))
.catch(error => this.handleError(error));
}
export class SubscriptionManager {
private subscriptions: any[] = [];
public add(sub: any) {
this.subscriptions.push(sub);
}
public destroy(): void {
this.subscriptions.forEach((sub: any) => sub.unsubscribe());
this.subscriptions = [];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment