I am having a bit of difficulty with @ngrx/data and am hoping one of you Geniuses out there can help me out.
I have an entity collection and would like to store some additional information to reduce round trips to the server and also reduce redundant loading. I have a table of data and only want to load one page at at time into the entity collection, in order to be able to do this I would like to add additional meta data to my collection so that I know when to load more data. E.g. when I reach the end of the loaded data load more (pagination would need to know how many records exist and how many have been loaded).
According to the documentation I can add additionalCollectionState but need some way of updating the new state properties.
I figured I would copy/paste the example code that they have as a base and modify it to reflect my own properties.. the problem is that I immediately get a typescript error on the =>Action
Generic type 'Action' requires 1 type argument(s)
export class AdditionalPersistenceResultHandler extends DefaultPersistenceResultHandler {
handleSuccess(originalAction: EntityAction): (data: any) => Action {
const actionHandler = super.handleSuccess(originalAction);
// return a factory to get a data handler to
// parse data from DataService and save to action.payload
return function(data: any) {
const action = actionHandler.call(this, data);
if (action && data && data.foo) {
// save the data.foo to action.payload.foo
(action as any).payload.foo = data.foo;
}
return action;
};
}
}
I'm also not sure if this is the right way to go about this or am I going about this too complicated, could I "simply" update the additional collection state manually somehow (in my dataservice call getWithQuery() ) and if so what would be the best/recommended approach.
Cheers and Thanks
Gary
UPDATE
After Andrew pointed out my obvious import mistake I have now implemented the result handler but get the following error
ERROR in Error during template compile of 'AdditionalPropertyPersistenceResultHandler'
Class AdditionalPropertyPersistenceResultHandler in D:/dev/angular/ng-vet/src/app/treatments/services/treatments-entity-result-handler.ts extends from a Injectable in another compilation unit without duplicating the decorator
Please add a Injectable or Pipe or Directive or Component or NgModule decorator to the class.
which doesn't make any sense considering that the stackblitz doesn't have it and it works wonderfully.
my entityMetadataMap
const entityMetadata: EntityMetadataMap = {
TreatmentTemplate: {
entityDispatcherOptions: {
optimisticUpdate: true
}
},
Treatment: {
additionalCollectionState: {
totalRecords: 0
},
entityDispatcherOptions: {
optimisticUpdate: true
}
}
};
and providers:
providers: [
TreatmentsDataService,
TreatmentEntityService,
TreatmentTemplateResolver,
TreatmentTemplatesDataService,
TreatmentTemplateEntityService,
{
provide: PersistenceResultHandler,
useClass: AdditionalPropertyPersistenceResultHandler
},
{
provide: EntityCollectionReducerMethodsFactory,
useClass: AdditionalEntityCollectionReducerMethodsFactory
}
]
I basically copy pasted the methods from the stackblitz..
at ^8.0.2 of angular and ^8.6.0 of ngrx could that be the problem?
import { Action } from 'rxjs/internal/scheduler/Action';- Andrew Allen