简体   繁体   English

ngrx 数据 - 收集服务和数据服务,可观察的自定义端点

[英]ngrx data - collection service and data service, custom endpoint observable

I needed a custom PUT request related to my entity in ngrx/data, I would like to show it as I am not certain it is accurate... Say a I have a movie library and I can add tags to movies with PUT request.我需要一个与我在 ngrx/data 中的实体相关的自定义 PUT 请求,我想展示它,因为我不确定它是否准确......假设我有一个电影库,我可以使用 PUT 请求向电影添加标签。

My data service:我的数据服务:

export class MovieDataService extends DefaultDataService<typeof Movie> {
  constructor(http: HttpClient, httpUrlGenerator: MovieHttpUrlGenerator, logger: Logger) {
    super('Movie', http, httpUrlGenerator);
    logger.log('Created custom Movie EntityDataService');
  }

  // CUSTOM METHODS APART FROM AUTO-CRETED BY COLLECTION SERVICE 
  // Further handled in collection service
  addTagToMovie(movieId: number, tagId: number) {
    return this.execute(
      'PUT', 
      `https://external-api.com/movie/${movieId}/add_tag/${tagId}/`,
    )
  }

}

Collection-service代收服务

 constructor(
    EntityCollectionServiceFactory: EntityCollectionServiceFactory,
    private movieDataService: movieDataService,
  ) {
    this.movieCollectionService = EntityCollectionServiceFactory.create<typeof Movie>('Movie');
  }

  getMovies() { this.movieCollectionService.getAll(); }

  addTagToMovie(movieId: number, tagId: number) {
    if (movieId && tagId) {
      this.movieCollectionService.setLoaded(false)
      this.movieCollectionService.setLoading(true)

      this.movieDataService.addTagToMovie(movieId, tagId).pipe(
        tap((updatedMovie: typeof Movie) => {
          this.movieCollectionService.updateOneInCache(updatedMovie)
          this.movieCollectionService.setLoading(false)
          this.movieCollectionService.setLoaded(true)
        })
      ).subscribe()
    }

  }

Is this an appropriate way to achieve this?这是实现这一目标的合适方法吗? Also, will the subscribe on addTagToMovie cause a memory leak?另外,订阅 addTagToMovie 会导致 memory 泄漏吗? Without it, it does not trigger, other collection services methods need no subscribing (for instance getAll()) is it possible to implement it withou subscriibng as well?没有它,它不会触发,其他收集服务方法不需要订阅(例如 getAll())是否也可以在没有订阅的情况下实现它?

I tried the described above.我尝试了上述方法。

My understanding is that the subscribe need not happen on the service, instead just return an observable on the service, which will get subscribed on the component.我的理解是订阅不需要在服务上发生,而只是返回一个服务上的可观察对象,它将在组件上订阅。

Collection-service代收服务

 constructor(
    EntityCollectionServiceFactory: EntityCollectionServiceFactory,
    private movieDataService: movieDataService,
  ) {
    this.movieCollectionService = EntityCollectionServiceFactory.create<typeof Movie>('Movie');
  }

  getMovies() { this.movieCollectionService.getAll(); }

  addTagToMovie(movieId: number, tagId: number) {
    let output$ = of([]);
    if (movieId && tagId) {
      this.movieCollectionService.setLoaded(false)
      this.movieCollectionService.setLoading(true)

      output$ = this.movieDataService.addTagToMovie(movieId, tagId).pipe(
        tap((updatedMovie: typeof Movie) => {
          this.movieCollectionService.updateOneInCache(updatedMovie)
          this.movieCollectionService.setLoading(false)
          this.movieCollectionService.setLoaded(true)
        })
      )
    }
    return output$;
  }

In the component you can do在组件中你可以做

private subscription: Subscription = new Subscription();

ngOnInit() {
    this.subscription.add(
        this.collectionService.addTagToMovie(1,2).subscribe()
    );
}

ngOnDestroy() {
    this.subscription.unsubscribe();
}

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

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