繁体   English   中英

参照对象的可观察链

[英]Observable chain with reference to objects

我是Observable的新手,我不确定自己是否做对了。

这是场景。

a)订阅路由参数

b)当参数更改时,执行服务请求以获取可观察的数据

c)处理数据

d)然后对它们的依赖项进行更多请求并获取其依赖项可观察值。

我的问题是,在d)之后,我可以很好地获取它们的依赖项可观察值,但是我没有到已处理数据的映射来根据需要附加响应。

例:

this.route.queryParams.pipe(map((params) => {
  this.selectedTabIndex = +params['tabIndex'];
  return params
})).pipe(switchMap((params) => {
  return this.eventService.getEvent(params['eventID']);
})).pipe(map((event) => {
  this.event = event;
  this.selectedActivities = event.getActivities();
  return this.selectedActivities;
})).pipe(mergeMap((activities) => {
  return combineLatest(activities.map((activity) => {
    return this.eventService.getStreams(this.event.getID(), activity.getID(), ['Latitude', 'Longitude'])
  }))
})).pipe(map((activityStreams) => {
  // Here is my problem 
  // As you can see I can get well the streams for the above activities
  // But I don't have the activity that the streams belong to to attach them to
  debugger
})).subscribe()

因此,在最后一个管道中,结果如下所示:

在此处输入图片说明

但是正如您所看到的,我没有提到那些流数组最后应该附加到的活动。 这就是我想要做的。

也许我做的事情完全错了,所以我可以公开讨论。

在Sheshaj Awasthi的帮助下

我做了以下事情:

this.route.queryParams.pipe(map((params) => {
  this.selectedTabIndex = +params['tabIndex'];
  return params
})).pipe(switchMap((params) => {
  return this.eventService.getEvent(params['eventID']);
})).pipe(map((event) => {
  this.event = event;
  this.selectedActivities = event.getActivities();
  return this.selectedActivities;
})).pipe(map((activities) => {
  return activities.reduce((activityStreamPairArray, activity) => {
    activityStreamPairArray.push({
        activity: activity,
        activityStreams: this.eventService.getStreams(this.event.getID(), activity.getID(), ['Latitude', 'Longitude']),
      });
    return activityStreamPairArray
  }, [])
})).pipe(mergeMap((activityStreamPairArray) => {
  return combineLatest(activityStreamPairArray.reduce((flattenedArray, activityStreamPair) => {
    flattenedArray.push(of(activityStreamPair.activity), activityStreamPair.activityStreams);
    return flattenedArray
  }, []))
})).pipe(map((test) => {
  debugger

}))

我现在有

在此处输入图片说明

其中包含一个数组,每2个元素可以组合在一起。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM