Skip to content

HalModel

Mihael Safaric edited this page May 28, 2019 · 29 revisions

HalModel is an abstract class which enriches your models with the HAL functionalies.

constructor(resource: RawHalResource = {}, private datastore: DatastoreService)

Properties

  • endpoint: string

    • used for build resource URLs
    • readonly
    • can be set via ModelConfig decorator
    • default value: the value of endpoint property from the model decorator
    • NOTE: either this getter has to be implemented or endpoint must be specified in the model constructor
  • uniqueModelIdentificator: string

    • returns unique model identificator
    • default: URL to the resource
  • id: string

    • returns the model ID parsed from self link
  • networkConfig: NetworkConfig

    • returns network config specified on a model level
  • isSaved: boolean

    • return true if model is saved, else otherwise
  • selfLink: string

    • GET: returns URL to the resource
    • SET: sets selfLink property of the model if it is not already set

Methods

getHalDocumentClass

getHalDocumentClass<T extends this>(): HalDocumentConstructor<T>;
  • returns HalDocument class which is defined on the model level

getRelationshipUrl

getRelationshipUrl(relationshipName: string): string;
  • returns URL for fetching relationshipName relationship

getPropertyData

getPropertyData(propertyName: string): ModelProperty;

getEmbeddedResource

getEmbeddedResource(resourceName: string): RawHalResource | undefined;
  • returns HAL formatted object which represents a model (or models) under resourceName relationship
  • returns undefined if such object does not exist in the resource response
  • it searches for the object under resource[resourceName] and inside the embedded property, ie. resource._embedded[resourceName]

generatePayload

generatePayload(): object;
  • maps model attribute properties to a simple object and returns that object

save

save(): Observable<this>;
  • calls save method on a datastore service which then makes POST/PUT request for the current model
  • see DatastoreService

Decorators

Model properties must be decorated with one of the following decorator: