参照对象的可观察链

时间:2018-12-12 12:25:33

标签: javascript rxjs observable

我是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()

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

enter image description here

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

也许我做的事情完全错误,所以我愿意讨论。

1 个答案:

答案 0 :(得分:0)

在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

}))

现在我有

enter image description here

由一个数组组成,每2个元素可以组合在一起。