platform icon indicating copy to clipboard operation
platform copied to clipboard

ngrx/data documentation for Sub-class EntityServices for application class convenience

Open richardbrammer opened this issue 7 years ago • 1 comments

I try to create a Sub-class EntityServices for application class convenience according to the NGRX/data documentation at https://ngrx.io/guide/data/entity-services#sub-class-entityservices-for-application-class-convenience

Unfortunately the provided example doesn't seem to work with the version of ngrx/data 8.5.2. The example looks like:

@Injectable()
  export class AppEntityServices extends EntityServicesBase {
    constructor(
      public readonly store: Store<EntityCache>,
      public readonly entityCollectionServiceFactory: EntityCollectionServiceFactory,

      // Inject custom services, register them with the EntityServices, and expose in API.
      public readonly heroesService: HeroesService,
      public readonly villainsService: VillainsService
    ) {
      super(store, entityCollectionServiceFactory);
      this.registerEntityCollectionServices([heroesService, villainsService]);
  }
}

When I use the example with my project, I get the following TypeScript error:

Error: (parameter) entityCollectionServiceFactory: EntityCollectionServiceFactory Expected 1 arguments, but got 2. ts(2554)

for the line when I call the parents constructor super(...) It looks like EntityServicesBases constructor is only accepting 1 argument of type EntityServicesElements. But how can I create my custom AppEntityService? I couldn't find any working example

richardbrammer avatar Nov 27 '19 13:11 richardbrammer

Well ... that's embarrassing.

What you cited is documentation from an earlier era in NgRx Data history. I'll be updating that page ASAP.

No need to wait for that update. The correct implementation would be as follows:

import { Injectable } from '@angular/core';
import { EntityServicesBase, EntityServicesElements } from '@ngrx/data';

import { SideKick } from '../../model';
import { HeroService, VillainService } from '../../services';

@Injectable()
export class AppEntityServices extends EntityServicesBase {
  constructor(
    elements: EntityServicesElements,

    // Inject custom services, register them with the EntityServices, and expose in API.
    readonly heroesService: HeroesService,
    readonly villainsService: VillainsService
  ) {
    super(elements);
    this.registerEntityCollectionServices([heroesService, villainsService]);
  }

  /** get the (default) SideKicks service */
  get sideKicksService() {
    return this.getEntityCollectionService<SideKick>('SideKick');
  }
}

wardbell avatar Feb 26 '20 04:02 wardbell