2016-10-06 7 views
0

私はIonic2/rc0でアプリケーションを開発しています。私は、現在のユーザをアプリ全体にわたって一貫性のあるものにする、単一のサービスでReplaySubjectを取得しました。それはすべてあなたが私のバックエンドはワンでのMongoDBを使用している見ることができるように正常に動作し、私はそれを購読し、MongoDBのRxjsワークフロー参考文献

this._user.Current.subscribe(user=>{ console.log(user)}); 

限り簡単にUserオブジェクトを取得することができ、ユーザーオブジェクトがこの

User { 
    confirmed:true 
    devices:["57f65werwe343bn8843f7h","7yr3243h5429hf2hjd"] 
    friends:["t245y53h65346htyh","356ytrer75dfhg43we56df"] 
    email:"[email protected]" 
    id:"57f6525e926bbc7615fc5c5c" 
    notification:false    
    password="$2a$04$.Fk/8eMj18ZrkfurbbdP4uT3yOs7Lb9db74GkNfgtABVY.ez2Q0I." 
    picture:"https://api.cescoferraro.xyz/kitty" 
    role:"master" 
    username:"cesco" 
} 

のように見えますhereと記載されているドキュメント参照との多対多リレーションシップ。

私はそれらのユーザデバイスに関するすべてのデータを表示したいデバイスのタブを作成しているが、私はcurrent.devicesのそれぞれについてthis._devices.infoを呼び出し、バックTrueDevices

@Component({ 
    template: ` 
      <ion-header> 
       <ion-navbar> 
        <ion-title>Tabs</ion-title> 
       </ion-navbar> 
      </ion-header> 
      <ion-content> 
       <h2>Device:list</h2> 

       <h2 *ngFor="let item of devices | async">{{item}}</h2> 

       <button ion-button (click)="readDevice()">Read Random Device</button> 
      </ion-content> 
` 
}) 
export class DeviceComponent { 
    devices: Observable<string[]>; 
    TrueDevices: Observable<Device[]>; 

    constructor(public _user: UserService, public _device: DeviceService) { 

     this._user.Current.subscribe(user=>{ this.devices = Observable.of(user.devices)}); 

     // Get current User 
     // call this._devices.info for each one of current.devices 
     // concat the result back to TrueDevices 
     this._user.Current 
      .subscribe((result) => { console.log(result) }); 

    } 

    readDevice(){ 
     console.log(this.devices); 
     this._device.info(this.devices.value[0]).subscribe(data=>console.log(data)) 
    } 
} 
に結果をCONCATする必要があります

友だちのタブなどと同じ手順を繰り返す必要があります。私は確かに魔法をやることになる2つの演算子があると確信していますが、私はrxjsにはかなり新しく、それらのすべてに精通していません。適切なアプローチは何ですか?

答えて

1

this._user.Current 
 
    .switchMap(user => Observable.from(user.devices)) // after this line, you have an Observable<string> 
 
    .mergeMap(device => this._device.info(device)) // each device will be mapped to another observable(or stream), and all the streams will be merged together 
 
    .toArray() // wait for all the streams to complete and reduce all the results into an array. 
 
    .subscribe(array => console.log(array));

またはギッター室に行く: https://gitter.im/Reactive-Extensions/RxJS

関連する問題