Effects Services
You may have noticed when looking at the entity definition code on the previous page that there is a property that we fill in named effectServiceToken
. As mentioned in the
We use a token here because it is the cleanest way to pass an injectable in a function. It points to a class that inherits from the abstract class EffectsService
. Our job is to implement each of the four methods defined in the abstract class. We use the same set of services for each of the four tabs because how we handle refreshing and garbage collection has no impact on how we interact with the server.
Top Effect Service
First, let's take a look at the
What you'll notice here is that we only actually implement one of the methods, loadByIds(ids: string[])
. This is because our top level entity will only ever retrieve data from the server. While we need to provide some implementation for the other methods to fulfill the contract with the abstract class, they don't have to access the server.
Locations Effect Service
The loadByIds(ids: string[])
, loadAll()
, save(entity: Location)
, and delete(entity: Location)
and all are relatively straightforward.
Departments Effect Service
The
DepartmentChildren Effect Service
The loadByIds(ids: string[])
method filters the ids by the table they are in using the
Once it has everything sorted, it creates a separate RxJS stream for each of the tables using the
Once we have all the streams created, we use forkJoin to call them in parallel and then create a map that combines all the results. You may wonder how these display in created order. The answer is that this data gets loaded into an NgRX entity and the order they display is determined by the order of the IDs in the field that holds the array of IDs for this entity in the parent row. We don't need to deal with order here.
Retrieving the Department Children Data
As mentioned above, the loadByIds(ids: string[])
function is what creates a stream for each of the tables it needs to retrieve data from. It does this by using the super()
with the HttpClient that is injected and the path on the server to call to get the data.
[!NOTE] Our code does not implement any error handling because the error handling needs to bubble up to the SmartNgRX Effect that calls the service in order for optimistic updates to work correctly. However, you might consider implementing retry logic or version detection for replication lag in your own application.