Skip to content

Commit

Permalink
share posts in blog post service with future subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
gs committed Jul 16, 2017
1 parent e969cb1 commit 7f5de30
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/app/app.routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const routes: Routes = [
];

@NgModule({
imports: [RouterModule.forRoot(routes, { enableTracing: true })],
imports: [RouterModule.forRoot(routes, { enableTracing: false })],
exports: [RouterModule],
})
export class AppRoutingModule { }
19 changes: 16 additions & 3 deletions src/modules/blog-posts/services/blog-posts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,27 @@ export class BlogPostService {

private serviceUrl: string;

private _posts: ReplaySubject<Array<BlogPost>>;
public readonly posts: Observable<Array<BlogPost>>;

constructor(private http: Http) {
this.serviceUrl = environment.endpoints.blogPostService;

this._posts = new ReplaySubject(1);
this.posts = this._posts.asObservable();

this._getPosts();
}

private _getPosts() {
this.http.get(`${this.serviceUrl}/posts`)
.map( (response) => response.json() )
.do( (posts) => this._posts.next(posts) )
.subscribe();
}

getPosts(): Observable<Array<BlogPost>> {
// return Observable.of(environment.posts);
return this.http.get(`${this.serviceUrl}/posts`)
.map((response) => response.json());
return this.posts;
}

}

0 comments on commit 7f5de30

Please sign in to comment.