Skip to content

Instantly share code, notes, and snippets.

@martinomburajr
Created September 21, 2017 12:23
Show Gist options
  • Select an option

  • Save martinomburajr/6520a1860497c8abc66aaa2f1b8cb4c7 to your computer and use it in GitHub Desktop.

Select an option

Save martinomburajr/6520a1860497c8abc66aaa2f1b8cb4c7 to your computer and use it in GitHub Desktop.
RxJS Operator Series
import { Component, OnInit } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import {Observable} from 'rxjs/Rx'
import * as firebase from 'firebase/app';
import 'rxjs/add/operator/map'
@Component({
selector: 'app-episode1',
templateUrl: './episode1.component.html',
styleUrls: ['./episode1.component.scss']
})
export class Episode1Component {
constructor(private afDB: AngularFireDatabase) {
this.example1();
}
/**
* Simple Map example
*
* @memberof Episode1Component
*/
example1() {
// Map user to entity
const user = new UserEntity();
this.afDB.object('/user/-KuL7eMizDwP503h8MZT')
.map(userObject => user.convertObjectToEntity(userObject['$key'], userObject))
.subscribe(userEntity => console.log(userEntity));
}
}
class UserEntity {
key: string;
name: string;
username: string;
bio: string;
convertObjectToEntity(key: string, object: {}): UserEntity {
const user = new UserEntity();
user.name = object['name']
user.key = key;
user.bio = object['bio'];
user.username = object['username'];
return user;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment