如何使用Observable参数对Observable进行排序? rxjs

时间:2016-11-13 14:16:45

标签: sorting angular rxjs

我正在尝试修改然后对列表进行排序。我完全是诺布:(

我能够用'combineLatest'做到这一点,但问题是我总是用'groupByDateAndTournament'下面的函数修改数组。 我的结果:

  1. 创建可观察的列表和排序参数(favoriteTournaments,teamRanks)
  2. 仅当可观察列表已更改
  3. 时,才使用'groupByAndTournament'修改列表
  4. 只有在obsevable favoriteTournament发生变化时才按喜欢排序
  5. 仅在可观察的teamRank发生变化时按等级排序

        this.subscription =  
        this.matchesService.getUpcoming()
        .merge(
        this.favoriteService.getFavoriteTournaments().flatMap((data) => {
            return {'favoriteTournaments': data}
        }),
        this.teamsService.getTeamRanking().flatMap((data) => {
            return {'teamRanks': data}
        })
    
    ).scan((acc, curr) => {
        let upcomingMatches;
        if (curr.upcoming) {
            upcomingMatches = this.groupByDateAndTournament(curr);
        }
        if (curr.favoriteTournaments) {
            upcomingMatches = this.sortByFavorite(curr)
        }
    
        if (curr.teamRanks) {
            upcomingMatches = this.sortByRank(curr);
        }
        return upcomingMatches;
    
    })
        .subscribe()
    

1 个答案:

答案 0 :(得分:0)

所以我用combineLatest

管理它
Observable.combineLatest(
        this.matchesService.getUpcoming().map((data)=> this.upcomingService.combineUpcomings(data)),
        this.favoriteService.getFavoriteTournaments(),
        this.teamsService.getTeamRanking(),
        (matches, favoriteTournaments, teamRanks) => this.upcomingService.prepareUpcomingMatches(matches, favoriteTournaments, teamRanks)
    ).subscribe((data)=>
        this.ngZone.run(() => {
            this.upcomingMatches = data;
            this.loading = false;
        })
    );

它仍有问题。PrepareUpcomingMatches函数在任何可观察量发生变化时重新运行。