-
Notifications
You must be signed in to change notification settings - Fork 1
HalModel
Mihael Safaric edited this page Jan 23, 2020
·
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 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
-
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
self
link
- returns the model ID parsed from
-
networkConfig
:NetworkConfig
- returns network config specified on a model level
-
isSaved
:boolean
- return
true
if model is saved,else
otherwise
- return
-
selfLink
:string
- GET: returns URL to the resource
- SET: sets
selfLink
property of the model if it is not already set
-
links
:RawHalLinks
- returns an object with the links to related resources
save(requestOptions?: RequestOptions, buildUrlFunction?: (model: this, urlFromModel: string) => string): Observable<this>;
- calls
save
method on a datastore service - see
DatastoreService
update(requestOptions?: RequestOptions, options: UpdateOptions<this> = {}): Observable<this>
- calls
update
method 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
-
delete(requestOptions?: RequestOptions, buildUrlFunction?: (model: this, urlFromModel: string) => string): Observable<void>;
- calls
delete
method on a datastore service which then makesDELETE
request for the current model - see
DatastoreService
getHalDocumentClass<T extends this>(): HalDocumentConstructor<T>;
- returns
HalDocument
class which is defined on the model level
getRelationshipUrl(relationshipName: string): string;
- returns URL for fetching
relationshipName
relationship
fetchRelationships(relationshipNames: string | Array<string>): Observable<this>;
- it uses
fetchModelRelationships
in the background, check docs there - fetches one or more model relationships
- returns the original model
- the original model is fetched from the local store
getRelationship<T extends HalModel>(relationshipName: string): T | HalDocument<T>;
- 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
getPropertyData(propertyName: string): ModelProperty;
- returns the information about
propertyName
property
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 theembedded
property, 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
Model properties must be decorated with one of the following decorator: