To use one of these methods in your component, simply inject your facade class and call the method in the constructor, an ngOnInit or similar handler, or a custom handler:
We highly recommend following NgRx best practices here to avoid subscribing directly to any observable on an entity facade. Instead, use the async
pipe from Angular in your templates, and follow container/presenter model for your component architecture:
Prefabricated facades in NgRx Auto-Entity expose all the necessary properties and methods to allow you to fully leverage all of the state, actions and selectors Auto-Entity manages. Once you have extended a class from the base facade class, you may leverage all of this functionality without any additional effort.
In addition to properties for each selection, entity facades also expose a number of methods that wrap NgRx action
dispatch calls. Each of these methods handles creation and subsequent dispatch of the appropriate generic action, including handling all the type information. The available methods on every facade include:
select(entity: TModel): Selects a single entity selectByKey(key: EntityIdentity): Selects a single entity by the specified key selectMany(entities: TModel[]): Selects the specified entities selectManyByKeys(keys: EntityIdentity[]): Selects entities by the specified keys deselect(): Deselects any previously selected entity deselectMany(entities: TModel[]): Deselects the specified entities deselectManyByKeys(keys: EntityIdentity[]): Deselects the specified entities by keys deselectAll(): Deselects all selected entities
load(keys: any, criteria?: any): Loads entity by the specified key loadMany(criteria: any): Loads many entities by the specified criteria loadAll(criteria?: any): Loads all entities loadPage(page: Page, criteria?: any): Loads a page of entities loadRange(range: Range, criteria?: any): Loads a range of entities create(entity: TModel, criteria?: any): Creates a new entity createMany(entities: TModel[], criteria?: any): Bulk creates entities update(entity: TModel, criteria?: any): Updates an entity updateMany(entities: TModel[], criteria?: any): Bulk updates entities replace(entity: TModel, criteria?: any): Replace an entity replaceMany(entities: TModel[], criteria?: any): Bulk replace entities delete(entity: TModel, criteria?: any): Delete an entity deleteMany(entities: TModel[], criteria?: any): Bulk delete entities clear(): Clears all state for the entity type and resets it to empty
All of these methods, as with the properties, are encapsulating functionality you may already be familiar with. In the past with plain old NgRx, you might have done something like the following:
This is in fact what all of the entity facade methods are doing for you. We simplify the above repetitive procedure so you may simply call a method with just the bare bones required arguments, rather than have to bring in the store, dispatch and construct new actions yourself:
The model type is tracked when you extend the facade base class with your own concrete implementation: