Advertisement
mrnethen

Untitled

Apr 10th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {OnDestroy} from "@angular/core";
  2. import {Subscription} from "rxjs/Rx";
  3.  
  4. export class SubscriptionsRemovingComponent implements OnDestroy{
  5.     private subscriptions: Subscription[] = [];
  6.  
  7.     ngOnDestroy():void {
  8.         this.clearSubscriptions();
  9.     }
  10.  
  11.     public clearSubscriptions() {
  12.         this.subscriptions.forEach((subscription: Subscription) => {
  13.             subscription.unsubscribe();
  14.         });
  15.     }
  16.  
  17.     public addSubscription(subscription: Subscription) {
  18.         this.subscriptions.push(subscription);
  19.     }
  20. }
  21.  
  22. class SelectableGroupList extends SubscriptionsRemovingComponent implements OnDestroy {
  23.     private groups:Group[] = [];
  24.     private searchResults: Group[] = [];
  25.  
  26.     @Output() groupsSelected: EventEmitter<Group[]> = new EventEmitter<Group[]>();
  27.     @Output() backAction = new EventEmitter<any>();
  28.     @Input() selectedGroups: Array<Group> = [];
  29.  
  30.     constructor(private groupsService: GroupsService,
  31.                 private searchService: SearchService) {
  32.         super();
  33.         this.groupsService.getGroups();
  34.         this.setSubscriptions();
  35.     }
  36.  
  37.     private setSubscriptions() {
  38.         super.addSubscription(
  39.             this.groupsService.selectedGroupsChanged
  40.                 .subscribe((selectedGroups:Group[]) => {
  41.                     this.groupsSelected.emit(selectedGroups);
  42.                     super.clearSubscriptions();
  43.                 })
  44.         );
  45.  
  46.         super.addSubscription(
  47.             this.groupsService.groupsChanged
  48.                 .subscribe((groups:Group[]) => {
  49.                     this.groups = this.excludeSelectedGroups(groups);
  50.                     this.searchResults = this.groups;
  51.                     super.clearSubscriptions();
  52.                 })
  53.         );
  54.     }
  55.  
  56.     ngOnDestroy() {
  57.         super.ngOnDestroy();
  58.  
  59.         this.groupsService.resetSelectedGroups();
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement