-
-
Save MikaAK/ccd57f28640564e8f50c17a449fe221b to your computer and use it in GitHub Desktop.
Subject Observer Example
This file contains 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 {Observer} from 'rxjs/Observer'; | |
import {BehaviorSubject} from 'rxjs/BehaviorSubject'; | |
interface IFilter { | |
name: string, | |
tags: CollectionMatcher | |
} | |
export class FilterService { | |
private _filters = new BehaviorSubject<IFilter>({ | |
name: '', | |
tags: { | |
values: [], | |
allMustMatch: false | |
} | |
}) | |
public changeFilters(filters) { | |
this._filters.next(filters) | |
} | |
public getFilters(prop?: string): BehaviorSubject<IFilter> { | |
if (prop) | |
return this._filters.map((filters: IFilter) => _.get(filters, prop)) | |
else | |
return this._filters | |
} | |
public subscribe(fn) { | |
return this._filters.subscribe(fn) | |
} | |
} |
This file contains 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
class ObserverImpl implements Observer { | |
public next(result: any) { | |
console.log(`result: ${result}`); | |
}; | |
public error(err: any) { | |
console.log(`error: ${err}`); | |
}; | |
public complete() { | |
console.log('Completed'); | |
} | |
} |
This file contains 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 {FilterService} from './FilterService'; | |
import {ObserverImpl} from './ObserverImpl'; | |
export class SomeClass { | |
private subscription: ISubscription; | |
constructor(private filterService: FilterService, private $scope: any) { | |
this.subscription = this.filterService.subscribe(new ObserverImpl()); | |
$scope.$on('$destroy', () => this.subscription.unsubscribe(); | |
} | |
/* moar stuff */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment