Trying to understand the ngrx/data entity data service example here, where it says "Creating entity data services". After showing that service, the docs go on to show how to use ngrx/data in components. The part of the component I'm interested in is this:
getHeroes() {
this.heroService.getAll();
}
The docs state that getAll()
initiates an HTTP request, but I'm not sure where or how this request is actually made. In the ngrx-data repo. It states to replace the heroService with the following code:
import { Injectable } from '@angular/core';
import {
EntityCollectionServiceBase,
EntityCollectionServiceElementsFactory
} from 'ngrx-data';
import { Hero } from '../core';
@Injectable({ providedIn: 'root' })
export class HeroService extends EntityCollectionServiceBase<Hero> {
constructor(serviceElementsFactory: EntityCollectionServiceElementsFactory) {
super('Hero', serviceElementsFactory);
}
}
The docs state ngrx-data handles getting and saving our data for us
. That's great, but I don't know where this getting data is happening. I cloned the repo, checked out the finish
branch and could not find something that is hitting endpoints.
E.g. the service used to call http.get(${api}/heroes)
for getAll()
to get all the heroes, but that's replaced by the above code, so where are these calls occurring?
I notice that the EntityCollectionServiceBase
has a getAll()
method. But where is the configuration of this service taking place to register the respective endpoints? I'm sure that I am missing something incredibly simple here.
Broadly speaking the EntityCollectionServiceBase
dispatches actions and via the persist effect in EntityEffects
the DefaultDataService
is called and maps the response to its success / fail actions. See Architecture Overview
Hence if you want to access / transform the data returned from the API you extend / replace the DefaultDataService
. You can register your custom data service using EntityDataService
which is basically a registry of data services that gets or creates the data service (uses default if none exists).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With