BaseViewModel

Source
import { BaseViewModel } from "@prestojs/viewmodel";

The base class all ViewModel classes will extend.

If you use the baseClass option the class passed must extend BaseViewModel.

API

new BaseViewModel(data)

Source
ParameterTypeDescription
*data__type

Methods

Clone this record, optionally with only a subset of the fields

ParameterTypeDescription
fieldNamesstring[]|FieldPath[]
ViewModelInterface

Given 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.

ParameterTypeDescription
*recordsViewModelInterface[]
FieldPath[]

Compares 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
ParameterTypeDescription
*recordViewModelInterface|null
boolean

Return the data for this record as a plain object

__type

Properties

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.

List of field names with data available on this instance.

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.

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

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 Snow
user._f.name
// CharField({ name: 'name', label: 'Label' });
user._f.name.value
// Jon Snow

Returns the primary key value(s) for this instance. This is to conform to the Identifiable interface.

Get the actual ViewModel class for this instance