BaseViewModel
SourceThe base class all ViewModel classes will extend.
If you use the baseClass option the class passed must extend
BaseViewModel
.
API
Constructor
new BaseViewModel(data)
SourceParameter | Type | Description | |
---|---|---|---|
* | data | __type |
Methods
clone(?fieldNames)
SourceClone this record, optionally with only a subset of the fields
Parameter | Type | Description | |
---|---|---|---|
fieldNames | string[]|FieldPath[] |
fieldPathIntersection(records)
SourceGiven records
return the paths that are common between them.
A naive solution is to just check _assignedFieldsDeep
:
const paths = intersectionBy(...assignedData[key].map(record => record._assignedFieldsDeep),p => flattenFieldPath(p).join('|'));
but that would fail if some records had a null value for a relation and others didn't.
This function handles nested records such that a null relation is ignored. For example if you received:
[{id: 1,nestedRecordId: null,nestedRecord: null,},{id: 2,nestedRecordId: 1,nestedRecord: {id: 1,name: 'Nested Record 1',},},{id: 3,nestedRecordId: 2,nestedRecord: {id: 2,name: 'Nested Record 2',otherField: 'Name',},}]
would result in
['id', 'nestedRecordId', ['nestedRecord', 'id'], ['nestedRecord', 'name']]
Noting that the first record has no nested fields (because they are null) and so get's ignored, and the last record has 'otherField' which the second doesn't so is excluded.
Parameter | Type | Description | |
---|---|---|---|
* | records | ViewModelInterface[] |
isEqual(record)
SourceCompares two records to see if they are equivalent.
- If the ViewModel is different then the records are always considered different
- If the records were initialised with a different set of fields then they are considered different even if the common fields are the same and other fields are all null
Parameter | Type | Description | |
---|---|---|---|
* | record | ViewModelInterface|null |
Properties
_assignedFieldPaths: ViewModelFieldPaths
The ViewModelFieldPaths
instance for this record. This is a unique instance based on the actual
assigned fields and can be compared to other instances to determine if the same fields are set.
_assignedFields: string[]
List of field names with data available on this instance.
_assignedFieldsDeep: string[]
Deep field names set on this record. If no relations are set this is the same as _assignedFields
.
A deep field is a field that is a relation to another model and is represented as an array, eg.
['group', 'name']
would be the the name
field on the group
relation.
_data: Object
The assigned data for this record. You usually don't need to access this directly; values for a field can be retrieved from the record directly using the field name
_f: {[fieldName: string]: Field}
Get fields bound to this record instance. Each field behaves the same as accessing it via ViewModel.fields but
has a value
property that contains the value for that field on this record.
This is useful when you need to know both the field on the ViewModel and the value on a record (eg. when formatting a value from a record
const user = new User({ name: 'Jon Snow' });user.name// Jon Snowuser._f.name// CharField({ name: 'name', label: 'Label' });user._f.name.value// Jon Snow
_key: PkFieldType
Returns the primary key value(s) for this instance. This is to conform to the Identifiable interface.
_model: ViewModel Class
Get the actual ViewModel class for this instance