-
Notifications
You must be signed in to change notification settings - Fork 1
HalModel
Mihael Safaric edited this page Sep 27, 2024
·
29 revisions
HalModel is an abstract class which enriches your models with the HAL functionalies.
constructor(resource: RawHalResource = {}, private datastore: DatastoreService, public rawResponse?: HttpResponse<any>)-
endpoint:string- used for building resource URLs
- readonly
- can be set via
ModelConfigdecorator - default value: the value of
endpointproperty from the model decorator - NOTE: either this getter has to be implemented or
endpointmust be specified in the model constructor
-
modelEndpoints:ModelEndpoints- used for building resource URLs instead of
endpoint - readonly
- if provided,
singleResourceEndpointis used when:- fetching a resource via the
findOnemethod - updating a resource via the
updatemethod
- fetching a resource via the
- if provided,
collectionEndpointis used when:- fetching resources via the
findmethod - creating a resource
- fetching resources via the
- both
singleResourceEndpointandcollectionEndpointsupport templated strings- templated parts of an URL will be replaced with
requestOptions.paramsandrequestOptions.routeParams- the difference between the two is that
routeParamswon't be sent in a request payload, they are used only for building templated URL
- the difference between the two is that
- templated parts of an URL will be replaced with
- used for building resource URLs instead of
-
type:string- return unique name of the model class specified in the model decorator
-
uniqueModelIdentificator:string- returns unique model identificator
- default: URL to the resource
- Guide: Overriding unique model identificator
-
id:string- returns the model ID parsed from
selflink
- returns the model ID parsed from
-
networkConfig:NetworkConfig- returns network config specified on a model level
-
isSaved:boolean- return
trueif model is saved,elseotherwise
- return
-
selfLink:string- GET: returns URL to the resource
- SET: sets
selfLinkproperty of the model if it is not already set
-
links:RawHalLinks- returns an object with the links to related resources
-
modelType:string- a unique model identificator used for differentiating between the models when defining relationships via
strings, seeCircular dependencies in DefiningModelRelationships
- a unique model identificator used for differentiating between the models when defining relationships via
save(requestOptions?: RequestOptions, options: CustomOptions = {}): Observable<this>;- calls
savemethod on a datastore service - see
DatastoreService
update(requestOptions?: RequestOptions, options: CustomOptions<this> = {}): Observable<this>- calls
updatemethod on a datastore service - makes a PATCH request with changed properties only
- NOTE: it works with Attribute model properties only (omits hasOne and hasMany relationships) (TODO: implement handling hasOne and hasMany relationships)
- options
-
specificFields: Array- optional
- if provided, only the listed fields are taken in consideration while generating a payload (if they are changed)
-
buildUrlFunction- function used for generating the final request URL
- optional
- default:
model.endpoint
-
transformPayloadBeforeSave- optional
- the function gets a generated payload as an argument and the value returned from the function will be the payload which will be sent in the request
-
delete(requestOptions?: RequestOptions, options: CustomOptions): Observable<void>;- calls
deletemethod on a datastore service which then makesDELETErequest for the current model - see
DatastoreService
getHalDocumentClass<T extends HalModel<P>, P extends Pagination>(): HalDocumentConstructor<T, P>;- returns
HalDocumentclass which is defined on the model level
getRelationshipUrl(relationshipName: string): string;- returns URL for fetching
relationshipNamerelationship -
relationshipNamehas to be defined in a model, otherwise,nullwill be returned even if the relationship is present in the model links
fetchRelationships(relationships: string | RelationshipRequestDescriptor | Array<string | RelationshipRequestDescriptor>): Observable<this>;- it uses
fetchModelRelationshipsin the background, check docs there - fetches one or more model relationships
- returns the original model
- the original model is fetched from the local store
-
requestOptions- optional
- options which will be passed to all requests
getRelationship<T extends HalModel<P>>(relationshipName: string): T | HalDocument<T, P>;- it returns a HAL model (for has-one relationships) or a HAL document (for has-many relationships)
- it does NOT make an API call
- if the resource is not in the local store, undefined will be returned
refetch(includeRelationships?: Array<string | RelationshipRequestDescriptor>, requestOptions?: RequestOptions): Observable<T>;- re-fetches the model with provided options
getPropertyData(propertyName: string): ModelProperty;- returns the information about
propertyNameproperty
getEmbeddedResource(resourceName: string): RawHalResource | undefined;- returns HAL formatted object which represents a model (or models) under
resourceNamerelationship - returns
undefinedif such object does not exist in the resource response - it searches for the object under
resource[resourceName]and inside theembeddedproperty, ie.resource._embedded[resourceName]
generatePayload(options: GeneratePayloadOptions): object;- maps model attribute properties to a simple object and returns that object
generateHeaders(): object;- maps model header attribute properties to a simple object and returns that object
populateModelMetadata(sourceModel): void;- replaces source model data with a new one
Model properties must be decorated with one of the following decorator: