RelatedViewModelField
SourceDefine a field that references another ViewModel
This requires two things:
1) The ViewModel to reference 2) The field on the source ViewModel that contains the ID for the relation
In the following example User
has a Group
as a relation. The id for the
connected group is stored on the groupId
field:
class Group extends viewModelFactory({name: new CharField(),}) {}class User extends viewModelFactory({name: new CharField(),groupId: new IntegerField(),group: new RelatedViewModelField({to: Group,sourceFieldName: 'groupId',}),}) {}
You can then fetch the data - including the relations - from the cache:
Group.cache.add({ id: 1, name: 'Staff' });User.cache.add({ id: 1, name: 'Bob', groupId: 1 });User.cache.get(['name', 'group']);// { id: 1, name: 'Bob', groupId: 1, group: { id: 1, name: 'Staff' }}
The to
field can also be a function to support circular references:
class Group extends viewModelFactory({name: new CharField(),ownerId: new IntegerField(),owner: new RelatedViewModelField({to: () => User,sourceFieldName: 'ownerId',}),}) {}class User extends viewModelFactory({name: new CharField(),groupId: new IntegerField(),group: new RelatedViewModelField({to: Group,sourceFieldName: 'groupId',}),}) {}
You can query the circular relations as deep as you want:
Group.cache.add({ id: 1, name: 'Staff', ownerId: 1 });User.cache.add({ id: 1, name: 'Bob', groupId: 1 });User.cache.get(['name', 'group', ['group', 'owner'], ['group', 'owner', 'group']);// {// id: 1,// name: 'Bob',// groupId: 1,// group: {// id: 1,// name: 'Staff',// ownerId: 1,// owner: {// id: 1,// name: 'Bob',// groupId: 1,// group: {// id: 1,// name: 'Staff',// ownerId: 1,// }// },// },// }
to
can also be a a function that returns a Promise. This is useful to
lazy load modules:
class Subscription extends viewModelFactory({userId: new IntegerField(),user: new RelatedViewModelField({sourceFieldName: 'userId',to: async () => {const User = await import('./User').default;return User;}})}) {}
NOTE: When you return a promise you have to call resolveViewModel
on
that field before it's usable:
await Subscription.fields.user.resolveViewModel()
Failure to do this will result in an error being thrown the first time it's accessed.
If you have multiple values use ManyRelatedViewModelField instead.
API
Constructor
new RelatedViewModelField(props)
SourceParameter | Type | Description | |
---|---|---|---|
props.asyncChoices | AsyncChoicesInterface | Asynchronous choices for this field. Only one of | |
props.blank | boolean | Is this field allowed to be assigned a blank (null, undefined, "") value? Defaults to false | |
props.blankAsNull | boolean | Frontend values are often stored as strings even if they are not stored like that
in a backend (eg. database). Depending on your backend implementation it may expect
empty values to be represented as | |
props.choices | Map|[SingleValueT, string][] | Choices for this field. Should be a mapping of value to the label for the choice. | |
props.defaultValue | BaseRelatedViewModelValueType|null| Function | Default value for this field. This can either be a function that returns a value or the value directly. | |
props.helpText | string | Optional help text for this field that might be shown on a form | |
props.label | string | Label for this field. If not specified will be generated from the name. | |
props.readOnly | boolean | True if field should be considered read only (eg. excluded from forms) | |
props.writeOnly | boolean | True if field should be considered write only (eg. excluded from detail views) | |
* | props.sourceFieldName | string | The name of the field on the ViewModel that stores the ID for this relation |
* | props.to | Function |ViewModel Class |
Methods
toJS(value)
SourceConverts the linked record to a plain javascript object
Parameter | Type | Description | |
---|---|---|---|
* | value | RelatedViewModelValueType |
Static Properties
fieldClassName: string
Inherited Methods
format(value)
SourceFormat the value for displaying in a form widget. eg. This could convert a Date
into
a localized date string
Parameter | Type | Description | |
---|---|---|---|
* | value | RelatedViewModelValueType |
isEqual(value1,value2)
SourceCompares to relations for equality - if the ViewModel has the same data this returns true
Parameter | Type | Description | |
---|---|---|---|
* | value1 | RelatedViewModelValueType | |
* | value2 | RelatedViewModelValueType |
parse(value)
SourceParse a value received from a form widget onChange
call. eg. This could convert a localized date string
into a Date
.
Parameter | Type | Description | |
---|---|---|---|
* | value | FieldDataMappingRaw|null |
One of the following:
RelatedViewModelValueTypeOR
nullresolveViewModel()
SourceResolves the ViewModel this field links to. This is necessary as the ViewModel might be a dynamic import that hasn't yet loaded.
This needs to be called manually before to
can be accessed.
toString()
SourceInherited Properties
_isResolvingDeps: boolean
asyncChoices: AsyncChoicesInterface
Async choices for this field.
blank: boolean
Is this field required when saving a record?
blankAsNull: boolean
If true an empty string value should be converted to a null value
boundRecord: ViewModel
When accessed on a bound field will return the current instance of the ViewModel the field is bound to.
If called on an unbound field then this will always be undefined and a warning will be raised.
choices: Map
defaultValue: Promise<RelatedViewModelValueType|null|undefined>|RelatedViewModelValueType|null|undefined
Get the default value for this field.
helpText: string
Help text that can be displayed with the form widget
isBound: boolean
Returns true if field is bound to a ViewModel instance. When a field is bound to a instance the value for that field is accessible on the 'value' property.
label: string
Label that can be displayed as the form label for a widget
If not specified will be generated from name
.
many: boolean
model: ViewModel Class
The ViewModel class this field is attached to.
This will throw an error if the field is not attached to a model.
name: string
The name of this field.
This will throw an error if the field is not attached to a model.
readOnly: boolean
Indicates this field should only be read, not written. Not enforced but can be used by components to adjust their output accordingly (eg. exclude it from a form or show it on a form with a read only input)
sourceField: Field
sourceFieldName: string
to: ViewModel Class
Get the ViewModel this related field is to.
If to
was defined as a function returning a Promise
then you must call resolveViewModel
and wait for the returned Promise
to resolve before accessing this otherwise an error will be thrown
value: RelatedViewModelValueType
When isBound
is true this will return the current value of this field on the bound ViewModel.
Otherwise will always be undefined.
writeOnly: boolean
Indicates this field should only be written only and is not intended to be read directly. This is not enforced but can be used by components to adjust their output accordingly (eg. exclude it from a detail view on a record)