11import { Injectable } from '@angular/core' ;
22import * as Rx from 'rxjs/Rx' ;
33
4+ import firebase = require( 'nativescript-plugin-firebase' ) ;
45import { Cost } from '../../common/protocol' ;
56
67@Injectable ( )
@@ -9,25 +10,45 @@ export class CostService {
910 private _costs : Rx . Observable < Cost [ ] > ;
1011
1112 constructor ( ) {
12- this . _costsSource = new Rx . BehaviorSubject < Cost [ ] > ( this . costsCollection ) ;
13- this . _costs = this . _costsSource . asObservable ( ) ;
13+ this . _costsSource = new Rx . BehaviorSubject < Cost [ ] > ( [ ] ) ;
14+ this . _costs = this . _costsSource . asObservable ( ) ;
1415 }
1516
16- private costsCollection = new Array < Cost > (
17- { id : 0 , quantity : 310 , type : "Products" , changesDate : "Jul 01 2017" , isFavorite : false } ,
18- { id : 1 , quantity : 25 , type : "Products" , changesDate : "Aug 25 2017" , isFavorite : false } ,
19- { id : 2 , quantity : 127 , type : "ForHome" , changesDate : "Aug 30 2017" , isFavorite : false } ,
20- { id : 3 , quantity : 80 , type : "Products" , changesDate : "Aug 29 2010" , isFavorite : false } ,
21- { id : 4 , quantity : 32 , type : "Products" , changesDate : "Nov 13 2017" , isFavorite : false } ,
22- { id : 5 , quantity : 235 , type : "Products" , changesDate : "Jan 31 2017" , isFavorite : false } ,
23- { id : 6 , quantity : 98 , type : "ForHome" , changesDate : "Feb 20 2017" , isFavorite : false } ,
24- { id : 7 , quantity : 105 , type : "Products" , changesDate : "Oct 01 2017" , isFavorite : false } ,
25- { id : 8 , quantity : 80 , type : "Products" , changesDate : "Aug 29 2017" , isFavorite : false } ,
26- { id : 9 , quantity : 75 , type : "Products" , changesDate : "Aug 08 2017" , isFavorite : false } ,
27- { id : 10 , quantity : 235 , type : "Products" , changesDate : "Aug 26 2017" , isFavorite : false } ,
28- { id : 11 , quantity : 264 , type : "ForHome" , changesDate : "May 30 2017" , isFavorite : false } ,
29- { id : 12 , quantity : 78 , type : "Products" , changesDate : "April 04 2017" , isFavorite : false }
30- ) ;
17+ // private costsCollection = new Array<Cost>(
18+ // { id: 0, quantity: 310, type: "Products", changesDate: "Jul 01 2017", isFavorite: false },
19+ // { id: 1, quantity: 25, type: "Products", changesDate: "Aug 25 2017", isFavorite: false },
20+ // { id: 2, quantity: 127, type: "ForHome", changesDate: "Aug 30 2017", isFavorite: false },
21+ // { id: 3, quantity: 80, type: "Products", changesDate: "Aug 29 2010", isFavorite: false },
22+ // { id: 4, quantity: 32, type: "Products", changesDate: "Nov 13 2017", isFavorite: false },
23+ // { id: 5, quantity: 235, type: "Products", changesDate: "Jan 31 2017", isFavorite: false },
24+ // { id: 6, quantity: 98, type: "ForHome", changesDate: "Feb 20 2017", isFavorite: false },
25+ // { id: 7, quantity: 105, type: "Products", changesDate: "Oct 01 2017", isFavorite: false },
26+ // { id: 8, quantity: 80, type: "Products", changesDate: "Aug 29 2017", isFavorite: false },
27+ // { id: 9, quantity: 75, type: "Products", changesDate: "Aug 08 2017", isFavorite: false },
28+ // { id: 10, quantity: 235, type: "Products", changesDate: "Aug 26 2017", isFavorite: false },
29+ // { id: 11, quantity: 264, type: "ForHome", changesDate: "May 30 2017", isFavorite: false },
30+ // { id: 12, quantity: 78, type: "Products", changesDate: "April 04 2017", isFavorite: false }
31+ // );
32+
33+ getCosts ( ) {
34+ firebase . addValueEventListener ( this . onValueEvent , "/money-tracker-2cd33" ) . then (
35+ function ( listenerWrapper ) {
36+ var path = listenerWrapper . path ;
37+ var listeners = listenerWrapper . listeners ; // an Array of listeners added
38+ // you can store the wrapper somewhere to later call 'removeEventListeners'
39+ }
40+ ) ;
41+
42+ firebase . query ( ( value ) => { console . log ( value ) ; } , '/money-tracker-2cd33' , { orderBy : { type : 1 } } ) . then ( ( val ) => {
43+ console . log ( "Val: " + val ) ;
44+ } , ( error ) => {
45+ alert ( "Error: " + error ) ;
46+ } ) ;
47+ }
48+
49+ onValueEvent ( result ) {
50+ console . log ( JSON . stringify ( result ) ) ;
51+ } ;
3152
3253 public get costs ( ) : Rx . Observable < Cost [ ] > {
3354 return this . _costs ;
0 commit comments