Options
All
  • Public
  • Public/Protected
  • All
Menu

Class FiltersService

Used for declarative building of objects which represent valuable state of target object.

Since this declaration is pretty abstract and hard to understand, let's look at a specific sample.

Typical usage of this service is something like this:

     class EndUserClass {
        @filter
        public parameter1 = 'Hey';
        @filter
        public parameter2 = 'There';
     }
     let endUserClassInstance = new EndUserClass();
     let filterService = new FilterService(endUserClassInstance);

Now we can use created filtersService instance for several cases:

  • by calling getRequestState we can get serializable representation of object state
      {
         parameter1: 'Hey',
         parameter2: 'There'
      }
  • by calling resetValues we can reset values of annotated properties to initial values (or to what was specified in FilterConfig.defaultValue property).
  • by calling applyParams we can automatically apply any set of values to annotated properties (we can pass queryString object to automatically apply values from it, for example).
  • by calling getRequestState with some filters we can "query" the state of filters and save it to the settings storage, for example.
  • by calling registerFilterTarget you can add any count of additional objects and get their composed state via getRequestState method as well as process them all with resetValues and applyParams.

Hierarchy

  • FiltersService

Index

Constructors

constructor

Properties

Protected appliedFiltersMapInternal

appliedFiltersMapInternal: Map<any, FilterConfig[]> = new Map<object, FilterConfig[]>()

Internal implementation of appliedFiltersMap

Protected filtersMapBuilded

filtersMapBuilded: boolean = false

Specifies was appliedFiltersMap already constructed or not.

Static filterPropertiesMap

filterPropertiesMap: Map<any, FilterConfig[]> = new Map<any, FilterConfig[]>()

Global collection of all filters configuration.

Used to build appliedFiltersMap for concrete set of objects that were registered as target objects via registerFilterTarget for concrete service instance.

Accessors

appliedFiltersMap

Methods

applyParams

  • applyParams(params: object): void

Private buildFilterTargetMap

  • buildFilterTargetMap(target: object): void
  • Builds map of settings for passed target object.

    Parameters

    • target: object
      • [id: string]: any

    Returns void

Private buildFiltersMap

  • buildFiltersMap(): void
  • Builds final map of settings for objects that were registered as target objects.

    This method is called automatically before first usage of appliedFiltersMap.

    Returns void

destroy

  • destroy(): void

getRequestState

  • getRequestState(filterFn?: function): any
  • Goes through all target properties of all target objects and applies their values to one resulted object literal.

    Typical usage of this method is building request to send it to the server.

    Names of properties in result object depends on FilterConfig.parameterName. Final values would be constructed by buildFilterValue method.

    Parameters

    • Optional filterFn: function

      optional function to filter applied values.

        • (config: FilterConfig, proposedValue: any, targetObject: object): boolean
        • Parameters

          • config: FilterConfig
          • proposedValue: any
          • targetObject: object

          Returns boolean

    Returns any

    resulted object literal.

registerFilterTarget

  • registerFilterTarget(...targets: any[]): void
  • Registers passed object as target object for current service instance.

    getRequestState method will compose result from objects that were registered by this method.

    applyParams and resetValues methods processes registered objects that were registered by this method.

    Parameters

    • Rest ...targets: any[]

      object(s) to register as target object.

    Returns void

removeFilterTarget

  • removeFilterTarget(...targets: any[]): void
  • Removes passed object from target objects collection for current service instance.

    This means that getRequestState, applyParams and resetValues methods stops to process this objects.

    Parameters

    • Rest ...targets: any[]

      object(s) to remove from collection of target object.

    Returns void

resetValues

  • resetValues(): void
  • Goes through all target properties of all target objects and sets their values to configured default.

    Default value will be determined as:

    This method performs next actions:

    • If value specified as FilterConfig.defaultValue is function it will be called with target object as this scope. If specified value is not a function it will be used by itself.
    • Result of previous step will be cloned by {@link cloneLiteral} function to avoid possible reference types problems.
    • If FilterConfig.parseFormatter method is specified it will be called with previous step result as parameter.
    • Result of previous steps is applied as value to target property.

    Returns void

Static Protected buildFilterValue

  • buildFilterValue(target: object, value: any, config: FilterConfig): object
  • Used to build resulted value of target property based on specified FilterConfig.

    This method is used by getRequestState and also calls oneself for the case of array values.

    In addition to FilterConfig configuration, this method checks if target property has method toRequest(). If so this method will be used to get serialized value.

    This convention has sense in several scenarios:

    • Serialization of complex object can be performed by it's own method which was declared once instead of copy-paste it in FilterConfig.serializeFormatter declarations.
    • Very tricky but sometimes useful usage of this convention is to declare toRequest in Date prototype.

    This gives ability to easily send Date objects to the server in appropriate format which server can apply or, for example, always send UTC-dates.

    But be accurate with this approach. There's a lot of problems with extending of embedded types.

    Parameters

    • target: object

      target object which holds specified target property. Used as this scope for FilterConfig.serializeFormatter method.

    • value: any

      raw value of target property.

    • config: FilterConfig

      filter configuration for target property.

    Returns object

Static registerFilterConfig

  • registerFilterConfig(targetType: object, propertyConfig: FilterConfig): void
  • Used to register type property as target property with specified filter config for later usage with FiltersService.

    This method is called by filter annotation, for example.

    Parameters

    • targetType: object

      type definition that contains specified property declaration.

    • propertyConfig: FilterConfig

      configuration for property as a filter.

    Returns void

Generated using TypeDoc