简体   繁体   English

参照对象的可观察链

[英]Observable chain with reference to objects

I am new to the Observables I am not sure if I am doing this right. 我是Observable的新手,我不确定自己是否做对了。

Here is the scenario. 这是场景。

a) Subscribe to route params a)订阅路由参数

b) When the params change do a service request to fetch the data observable b)当参数更改时,执行服务请求以获取可观察的数据

c) Process the data c)处理数据

d) Then do more requests for their dependencies and fetch their dependency observables. d)然后对它们的依赖项进行更多请求并获取其依赖项可观察值。

My problem is that after d) I can well fetch their dependency observables but I don't have a mapping to the processed data to attach the responses as needed. 我的问题是,在d)之后,我可以很好地获取它们的依赖项可观察值,但是我没有到已处理数据的映射来根据需要附加响应。

Example: 例:

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

So at the last pipe the result looks like: 因此,在最后一个管道中,结果如下所示:

在此处输入图片说明

But as you can see I don't have the reference to the activity that those stream array should be attached to in the end. 但是正如您所看到的,我没有提到那些流数组最后应该附加到的活动。 And that is what I want todo. 这就是我想要做的。

Perhaps I am doing things completely wrong so I am open to discussion. 也许我做的事情完全错了,所以我可以公开讨论。

With a little help from Sheshaj Awasthi 在Sheshaj Awasthi的帮助下

I did the following: 我做了以下事情:

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

}))

Now I have 我现在有

在此处输入图片说明

Which consists of an array where every 2 elements can be combined. 其中包含一个数组,每2个元素可以组合在一起。

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

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