Created
August 10, 2016 11:49
-
-
Save MichaelSolati/edcd30fe90f9ca202471c6b29e558a0c to your computer and use it in GitHub Desktop.
Angular2-Meteor Observable Service
This file contains hidden or 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 { Injectable } from '@angular/core'; | |
import { Meteor } from 'meteor/meteor'; | |
import { MeteorComponent } from 'angular2-meteor'; | |
import { BehaviorSubject } from 'rxjs/BehaviorSubject'; | |
// Collections | |
import { Posts } from '/imports/collections/posts/collection'; | |
/** | |
* @class PostsService | |
* @constructor | |
*/ | |
@Injectable() | |
export class PostsService extends MeteorComponent { | |
posts = new BehaviorSubject<Any>(null); | |
/** | |
* @method constructor | |
*/ | |
constructor() { | |
super(); | |
this.subscribePosts(); | |
} | |
/** | |
* Subcription for posts for Posts service. | |
* @method subscribePosts | |
*/ | |
subscribePosts() { | |
this.subscribe('posts', () => { | |
this.autorun(() => { | |
this.posts.next(Posts.find().fetch()); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment