Last active
May 27, 2016 01:47
-
-
Save icfantv/2aca3eeeedae2768ed6454dbf699e2bd 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 {Subject} from 'rxjs/Subject'; | |
export class FilterService { | |
private subject: Subject = new Subject(); | |
private _name: string; | |
private _tags: CollectionMatcher = { | |
values: [], | |
allMustMatch: false | |
}; | |
get name(): string { | |
return this._name; | |
} | |
set name(name: string) { | |
this._name = name; | |
this.subject.next(null); | |
} | |
get tags(): CollectionMatcher { | |
return this._tags; | |
} | |
set tags(item: CollectionMatcher) { | |
this._tags = item; | |
this.subject.next(null); | |
} | |
subscribe(observer: Observer): ISubscription { | |
return this.subject.subscribe(observer); | |
} | |
} |
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