MongoDB 文档参考的 Rxjs 工作流
Rxjs workflow for MongoDB Document References
我正在 Ionic2/rc0 上开发应用程序。我在单例服务上获得了 ReplaySubject,该服务使当前用户在整个应用程序中保持一致。一切正常,我可以订阅它并像
一样简单地获得一个用户对象
this._user.Current.subscribe(user=>{ console.log(user)});
用户对象如下所示
User {
confirmed:true
devices:["57f65werwe343bn8843f7h","7yr3243h5429hf2hjd"]
friends:["t245y53h65346htyh","356ytrer75dfhg43we56df"]
email:"francescoaferraro@gmail.com"
id:"57f6525e926bbc7615fc5c5c"
notification:false
password="a$.Fk/8eMj18ZrkfurbbdP4uT3yOs7Lb9db74GkNfgtABVY.ez2Q0I."
picture:"https://api.cescoferraro.xyz/kitty"
role:"master"
username:"cesco"
}
如您所见,我的后端正在使用 MongoDB 与文档引用的一对多关系,如 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))
}
}
我将需要对“朋友”选项卡等重复相同的过程。我很确定有几个运算符可以施展魔法,但我对 rxjs 还很陌生,并不熟悉所有运算符。什么是正确的方法?
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));
或者去 gitter room:
https://gitter.im/Reactive-Extensions/RxJS
我正在 Ionic2/rc0 上开发应用程序。我在单例服务上获得了 ReplaySubject,该服务使当前用户在整个应用程序中保持一致。一切正常,我可以订阅它并像
一样简单地获得一个用户对象 this._user.Current.subscribe(user=>{ console.log(user)});
用户对象如下所示
User {
confirmed:true
devices:["57f65werwe343bn8843f7h","7yr3243h5429hf2hjd"]
friends:["t245y53h65346htyh","356ytrer75dfhg43we56df"]
email:"francescoaferraro@gmail.com"
id:"57f6525e926bbc7615fc5c5c"
notification:false
password="a$.Fk/8eMj18ZrkfurbbdP4uT3yOs7Lb9db74GkNfgtABVY.ez2Q0I."
picture:"https://api.cescoferraro.xyz/kitty"
role:"master"
username:"cesco"
}
如您所见,我的后端正在使用 MongoDB 与文档引用的一对多关系,如 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))
}
}
我将需要对“朋友”选项卡等重复相同的过程。我很确定有几个运算符可以施展魔法,但我对 rxjs 还很陌生,并不熟悉所有运算符。什么是正确的方法?
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));
或者去 gitter room: https://gitter.im/Reactive-Extensions/RxJS