2017-09-06 9 views
0

私は基本的に、Firebaseのリアルタイムデータを変数に格納する共有サービスが必要です。様々なコンポーネントは、データストアから最新のデータを返すカスタムメソッドをサービスで呼び出します。これは、サービスからFirebaseObservableListを返すときのデータを取得する必要がなく、インスタントデータを意味します。あなたは共有データサービスAngularFire2

を理解してほしい今コンポーネント

import { Component, OnInit } from '@angular/core'; 
import { trigger, state, transition, style, animate } from '@angular/animations'; 
// Services 
import { TransactionService } from '../../../services/transaction.service'; 
@Component({ 
    selector: 'app-home', 
    templateUrl: './home.component.html', 
    styleUrls: ['./home.component.scss'], 
    animations: [ 
    trigger('fadeInOut', [ 
     transition('void => *', [ 
     style({ opacity: 0 }), 
     animate(150, style({ opacity: 1 })) 
     ]), 
     transition('* => void', [ 
     animate(150, style({ opacity: 0 })) 
     ]) 
    ]) 
    ], 
}) 
export class HomeComponent implements OnInit { 
    public bought: Array<any>; 
    public activeTab: string = 'recieved'; 
    constructor(private transactions: TransactionService) { 
    this.bought = this.transactions.getBought(); 
    } 
    ngOnInit() { 

    } 
} 

PSサービス

import { Injectable } from '@angular/core'; 
import { AngularFireDatabase, FirebaseObjectObservable, FirebaseListObservable } from 'angularfire2/database'; 
import { Observable } from 'rxjs/Observable'; 
import { AuthService } from './auth.service'; 

@Injectable() 
export class TransactionService { 

    protected userTransactions; 
    private bought: Array<any> = []; 
    constructor(private db: AngularFireDatabase, private auth: AuthService) { 
     // Listener to User Transactions 
     this.getUserTransactions(this.auth.getUserKey()) 
      .subscribe((transactions) => { 
       this.userTransactions = transactions; 
       this.processTransactions(); 
      }); 
    } 
    getUserTransactions(userKey: string): FirebaseListObservable<any[]> { 
     return this.db.list('/UserTransactions/' + userKey); 
    } 
    getCurrentUserTransactions() { 
     return this.userTransactions; 
    } 

    /** 
    * Process the Transactions and 
    * Categorize them 
    */ 
    processTransactions() { 
     this.rewarded = []; 
     this.userTransactions.forEach(transaction => { 
      let rewardType = transaction.transactiontype; 
      switch (rewardType) { 
       case "bought": 
        this.bought.push(transaction); 
        break; 


       default: 
        break; 
      } 
     }); 
    } 
    getBought() { 
     return this.bought; 
    } 

} 

を見てください:Guysがようやく見つかったので、私はAngular2と観測

答えて

0

にかなり新たなんだが自分自身の答え。最新のデータを格納するObservable(BehaviorSubject)を新たに作成しました。コンポーネントは、このオブザーバブルにサブスクライブします。

private _rewards: BehaviorSubject<any[]>; 

constructor(private db: AngularFireDatabase, private auth: AuthService) { 

     this.dataStore = { 
      rewards: [], 

     }; 

     this._rewards = <BehaviorSubject<any[]>>new BehaviorSubject([]); 

     this.subscribeToRewarded(); 

    } 

    subscribeToRewarded() { 
     this.db.list('< DB Path >', { 
      query: { 
       orderByChild: 'transactiontype', 
       equalTo: "reward" 
      } 
     }).subscribe((reward) => { 
      this._rewards.next(reward); 
     }); 
    } 

    getRewarded() { 
     return this._rewards.asObservable(); 
    }