Last active
December 6, 2016 02:16
-
-
Save sonicparke/6931ff00628f6dfb5519c8d4111dc2ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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