-
Notifications
You must be signed in to change notification settings - Fork 1
Attribute
Attribute
is used for decorating direct model properties. It takes one argument, AttributeOptions
, the type and the default values can be checked here
.
-
useClass
:boolean
|class
|() => class
- optional
- when omitted, the property value from the response will directly be used as a value of decorated model property
- when
true
, an instance of property type class will be used as a value of decorated model property. While instantiating an instance of type class, the raw value of the property from the response will be passed as a first parameter to the constructor - when
string
, the class will be searched inDatastoreService.modelTypes
based onmodelType
(see DefiningModelRelationships) - when
function
, the function will be called and it is expected of function to return a class which should be instantiated - when instance of
SimpleHalModel
orHalModel
, the value ofuseClass
property will be used as a class for instantiating the property value. While instantiating an instance of type class, the raw value of the property from the response will be passed as a first parameter to the constructor
-
transformResponseValue
:(rawAttribute: any) => any
- optional
- when provided, the raw property value from the response is passed to
transformResponseValue
function and returned value is saved to the model (instead of raw value from the response).
-
transformBeforeSave
:(rawAttribute: any) => any
- when provided, the model property value is passed to the
transformResponseValue
function and returned value is used while creating a payload for the request
- when provided, the model property value is passed to the
-
externalName
:string
- if your model property name and the property name fetched from the server are different, you can specify the external property name setting this property
- this name will be used when parsing a response from server and also when generating a payload for sending it to the server
-
excludeFromPayload
:boolean
- optional
- default:
false
- if
true
, the property won't be included in the payload of PUT/POST requests
Raw response fetched from the server
GET /User/1
{
"id": "1",
"name": "John",
"additionalInfo": {
"height": "170",
"weight": "75"
}
},
...
We have an additional class for additionalInfo
property:
class AdditionalUserInfo {
constructor(private userData) {}
get height() {
return this.userData.height;
}
}
class User extends HalModel {
@Attribute()
public name: string;
@Attribute()
public additionalInfo: AdditionalUserInfo;
}
While parsing the user response, under the hood, user.additionalInfo = rawResponse.additionalInfo
will be performed on the instance of a user model which means that user.additionalInfo
will be a simple object.
class User extends HalModel {
@Attribute()
public name: string;
@Attribute({
useClass: true
})
public additionalInfo: AdditionalUserInfo;
}
While parsing the user response, under the hood, user.additionalInfo = new AdditionalUserInfo(rawResponse.additionalInfo)
will be performed on the instance of a user model which means that user.additionalInfo
will be an instance of AdditionalUserInfo
class.
In this case AdditionalUserInfo
must extend SimpleHalModel
,
class AdditionalUserInfo extends SimpleHalModel{}
class SomeInfo {}
class User extends HalModel {
@Attribute()
public name: string;
@Attribute({
useClass: () => SomeInfo
})
public additionalInfo: AdditionalUserInfo;
}
While parsing the user response, under the hood, user.additionalInfo = new SomeInfo(rawResponse.additionalInfo)
will be performed on the instance of a user model which means that user.additionalInfo
will be an instance of SomeInfo
class.
class SomeInfo extends SimpleHalModel {}
class User extends HalModel {
@Attribute({
useClass: SomeInfo
})
public additionalInfo: AdditionalUserInfo;
}
The behaviour of this case is the same as in Case #3
. The difference is that instead of providing a function to useClass
, a concreate class is provided. The provided class must extend SimpleHalModel
.