2017-06-13 10 views
0

私はUser Objectを保持するFollow Observable Streamを作成しました。観測可能なストリームから特定のフィールドを取り出す方法は?

currentUserProfile$ = new BehaviorSubject<UserProfile>(null); 

私は、以下の方法で定義されている。この流れ初期化するために:

getCurrentUserProfile(userId):void{ 
    let profile:UserProfile= new UserProfile({}); 
    this._empService.getUserInfo(userId).subscribe(
     (response)=>{ 
      profile=response.profileData; 
      profile.isAuthenticated=true; 
      this.currentUserProfile$.next(profile); 
     }, 
     (error)=>{ 
      profile.isAuthenticated=true; 
      this.currentUserProfile$.next(profile); 
      console.error(error); 
     } 
    ); 
} 

を今、私は含まれていストリームこれらの属性のそれぞれのメソッドを作成します。例えば、

getUserRole(){ 
    let roles:Array<any>; 
    this.currentUserProfile$.subscribe((profile:UserProfile)=>{ 
     roles=profile.roles; 
    }); 
    return roles; 
} 

hasRole(role:string):boolean{ 
    let res:boolean; 
    this.currentUserProfile$.subscribe(
     (profile)=>{ 
     res=profile.roles.indexOf(role) !== -1; 
     } 
    ); 
    return res; 
} 

これより簡単な方法はありますか?事前に

おかげ

答えて

0

であるように私はむしろ、観察続けるだろう、と彼らは

getUserRole(){ 
    return this.currentUserProfile.map((profile:UserProfile)=> profile.roles); 
} 

hasRole(role:string):boolean{ 
    return this.currentUserProfile$.reduce((role) => 
     profile.roles.indexOf(role) !== -1; 
    ); 
} 
その上にいくつかの変換を適用
関連する問題