Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jamesmorgan/75a8e6c08208efcf4b9c to your computer and use it in GitHub Desktop.
Save jamesmorgan/75a8e6c08208efcf4b9c to your computer and use it in GitHub Desktop.
export class CompetitionsService {
// Create a Subect to observe
private _competitionsSource:Subject<Competition[]> = new Subject<Competition[]>();
// Publicly expose the Observable - called share() to allow multiple observers
competitionsChanged$:Observable<Competition[]> = this._competitionsSource.asObservable().share();
constructor(private _http:Http) {
_http.get('http://localhost:8080/competitions')
.map(res => res.json())
.subscribe(
data => {
// Call next() on on your source to propagrate to observers
this._competitionsSource.next(data)
},
err => console.error('Failed to load competitions', err),
() => console.log('Loaded competitions')
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment