NgRx Auto-Entity
Primary version
Primary version
  • NgRx Auto-Entity
  • Getting Started
    • Installation
    • Quick Start
    • Use your State!
      • Enhancing your Facade
      • Simplify your Component
    • From Scratch
      • App Interfaces
      • App Reducer
      • App State Module
      • Entity Model
      • Entity State
      • Update App State
      • Entity Service
      • Update App Module
  • Advanced Topics
    • Advanced Usage
      • Paradigm Changes
        • Models
        • Services
        • Service Providers
      • Taking Control
        • Integrating Custom Effects
      • Building Your Entities
        • Entity Names
        • Sort Comparers
        • Data Transforms
      • Building Your Entity States
        • The buildState() function
        • The buildFeatureState() function
        • The IEntityState Interface
        • The Selector Map
      • Generic Actions
        • Actions Now
        • Reusable Generic Actions
        • Custom Criteria
        • Loading Actions
          • Loading Entities
          • Loading Pages
          • Loading Ranges
        • Optional Loading
        • CURD Actions
        • Utility Actions
      • Correlation
      • Common Selectors
        • Exporting Selectors
      • Extra Selectors
      • Custom Selectors
        • Adding to Facades
        • Using Custom Selectors
      • Custom Effects
        • Composing Actions
        • Workflows
    • Leveraging Facades
      • Preparing Facades
      • The Interface: Selections
        • Using Facade Selections
        • Simplifying Further
      • The Interface: Activities
        • Using Facade Activities
      • So Little Code!
    • Utility Functions
      • Prototyping your Entities
        • Entity Making Performance
      • Entity Name Utilities
      • Entity Key Utilities
      • Entity Comparers
  • Examples
    • Sample Application
      • App Module
      • State
      • Models
      • Services
      • Facades
      • Container Components
      • Presentation Components
      • Modal Component
  • Documentation
    • Reference
  • Extras
    • Github Link
Powered by GitBook
On this page
  • Property or Accessor Method
  • Adding a Custom Property
  • Adding a Custom Accessor Method
Export as PDF
  1. Advanced Topics
  2. Advanced Usage
  3. Custom Selectors

Adding to Facades

Keeping your state and store centralized

If you do encounter the need to implement custom selectors, it is highly recommended that you integrate them into your entity facades, rather than consume them directly from components. This does of course assume you are using entity facades, and if you have foregone the use of facades then you may use custom selectors as you normally do with @ngrx.

Property or Accessor Method

After creating a custom selector, you will need to determine whether a property or accessor method is appropriate for this new selector in your facade. For non-parameterized selectors, a property is usually most appropriate.

For parameterized selectors, an accessor method may be more appropriate. If however the possible range of input to your parameters is limited then multiple properties that each encompass a particular use case might still be the best option.

Adding a Custom Property

Continuing on with our firstCustomer selector from the previous section, adding this to a CustomerFacade should be very strait forward:

customer.facade.ts
import {firstCustomer, CustomerFacadeBase} from 'state/customer.state';
import {Customer} from 'models';

export class CustomerFacade extends CustomerFacadeBase {
    firstCustomer$ = this.store.select(firstCustomer);
    
    constructor(store: Store<AppState>) {
        super(Customer, store);
    }
}

Adding a Custom Accessor Method

With a parameterized selector like our prior customerById example, we should use a method instead of a property to allow passing in the id of the customer to select:

customer.facade.ts
import {firstCustomer, customerByName, CustomerFacadeBase} from 'state/customer.state';
import {Customer} from 'models';

export class CustomerFacade extends CustomerFacadeBase {
    firstCustomer$ = this.store.select(firstCustomer);

    constructor(store: Store<AppState>) {
        super(Customer, store);
    }
    
    customerByName$(name: string): Observable<Customer> {
        return this.store.select(customerByName, {name});
    }
}
PreviousCustom SelectorsNextUsing Custom Selectors

Last updated 4 years ago