angular


How to handle dynamic properties within a class using Angular 2 with AngularFire2


I am wondering what is the best practice to handle dynamic properties within a class using Angular 2 with AngularFire2.
In my Firebase DB I have the following scheme:
$company
- users
-- $user
--- public
--- chats
--- events
---- $event
----- id
----- timestamp
- events
-- $event
--- id
--- timestamp
--- joiners
---- $joiner
----- uid
I implemented a User service:
// UserService
#Injectable()
export class UserService {
constructor(private af: AngularFire, private dbService: DatabaseService) {}
getUser(uid: string): Observable<User> {
console.log('Retrieveing user for uid ', uid);
let now: number = Date.now();
return Observable.combineLatest(
this.dbService.getNodeAsObjectObservable(true, `users/${uid}/public`),
this.dbService.getNodeAsListObservable(true, `users/${uid}/events`, {
query: {
orderByChild: 'timestamp',
startAt: now
}
})
).map(([info, joinedEvents]) => (new User({uid, info, joinedEvents})));
}
}
A Database service:
#Injectable()
export class DatabaseService {
public root: any;
public queue: any;
public company: any;
constructor(private af: AngularFire) {
console.log('DatabaseService constructor');
}
setCompany(company: string): void {
this.company = company;
}
// Utilities
getNodeAsListObservable(companyRelated: boolean, node: string, query?: any): FirebaseListObservable<any> {
console.log('DatabaseService - getNodeAsListObservable: with args ',
companyRelated, node, query
);
let ref: string;
companyRelated ? ref = this.company : ref = this.root;
if (query) {
return this.af.database.list(`${ref}/${node}`, query);
} else {
return this.af.database.list(`${ref}/${node}`);
}
}
getNodeAsObjectObservable(companyRelated: boolean, node: string): FirebaseObjectObservable<any> {
let ref: any;
companyRelated ? ref = this.company : ref = this.root;
return this.af.database.object(`${ref}/${node}`);
}
}
In my home.ts:
user: Observable<User>;
constructor(public navCtrl: NavController, private userService: UserService) {
console.log('HomePage constructor');
}
ngOnInit(): void {
// Retrieve current user
this.user = this.userService.getUser('abizhdiuh738937bj');
}
And finally my User class is defined as follows:
export interface IUser {
uid: string;
info: any;
//joinedEvents?:
}
export class User implements IUser {
uid: string;
info: any;
joinedEvents?: FirebaseListObservable<Event>;
constructor(obj?: any) {
if (obj.uid) this.uid = obj.uid;
if (obj.info) this.info = obj.info;
if (obj.joinedEvents) {
for (let i = 0; i < obj.joinedEvents.length; i++) {
// Q1: how to retrieve details of each event
}
}
}
}
For each joinedEvents I want to retrieve the details of it before displaying it in view.
So my question is what is the best way to pull this off?
I had several ideas:
in User constructor call a getEvent(event_id) function that returns a FirebaseObservableList
in getUser() (in UserService) once
this.dbService.getNodeAsListObservable(true, users/${uid}/events, {
query: {
orderByChild: 'timestamp',
startAt: now
}
})
is completed call a getEvent(event_id) function. How to even do that?
what should this function return an Observable? Ideally as I did for User I would like to use an Event class (but may be I am wrong to willing this...)
I am pretty lost on how to handle such encapsulated complexity. Also I will have to handle something pretty similar with chats each user belongs to... Hope you can help. Thanks

Related Links

Setting one parameter to a certain value upon entering a different parameter - NgSwitch?
Angular 2 page transition while leaving page
Adding the Multiselect feature to an Angular4 dropdown?
Add lines of code depending on folder structure - Precompiler?
Angular2 Rxjs Observable catch method compilation error
Partial file loader for translations [on hold]
How to pass data from modal to component?
Angular code diference between source code and debugger code
Angular2 cannot use the values of a subscribe in another subscribe (observable)
Angular 2 Passing parameters to the template through #Input() [closed]
All declarations of 'size' , 'prototype' must have identical modifiers
Accessing the Device Camera without getUserMedia [closed]
Template Error on ngFor and using pipe with parameter
Angular 2: Validators.compose() vs [] [duplicate]
angular 2 -spring boot
VelocityJS in a TypeScript project throws '../node_modules/#types/velocity-animate/index.d.ts is not a module'

Categories

HOME
google-analytics
phpunit
graphite
asynchronous
less
sendmail
custom-attributes
zurb-foundation
metal
javafx-2
visual-studio-mac
login
google-home
timezone
external
gemfire
codeception
worksheet-function
geometry
visio
zk
benchmarking
xamarin.mac
adodb-php
denial-of-service
uidocumentinteraction
build-automation
pex
ebcdic
wasapi
street-address
gsutil
direct3d9
web.py
war
toml
powerdns
homescreen
nerdtree
jes
pbs
carplay
ifttt
orika
jedi-vim
graylog
android-7.1-nougat
vorlon
azure-webjobssdk
rpath
python-wheel
countdowntimer
nested-function
typing
grid-layout
groovy-eclipse
tvml
network-monitoring
octopress
stunnel
adobe-flash-cs3
msbuildcommunitytasks
approximation
dynamic-ip
savefiledialog
pixman
ontouchevent
zenoss
ews-managed-api
syntactic-sugar
angularjs-ng-show
datacontext
bonjour
teamcity-7.1
graphael
contactscontract
nsfontmanager
scite
gideros
appmobi
addr2line
c89
wymeditor
dynamic-content
codeigniter-datamapper
wcsf
background-music
xcode4.1
iphone-sdk-4.1

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile