Skip to content

@jimka/typescript-ui / data / ModelRecord

Class: ModelRecord

Defined in: src/typescript/lib/data/ModelRecord.ts:26

A single data record managed by a store.

Tracks current field values, dirty state, and new / committed status.

Remarks

On construction the data snapshot is also stored as original so that reject() can restore the record to its last committed state without requiring a round-trip to the server.

Example

typescript
const record = store.getAt(0);
record?.set('age', 31);
console.log(record?.isDirty()); // true
record?.commit();               // clears dirty flag
// record?.reject();            // reverts to last committed snapshot

Constructors

new ModelRecord()

ts
new ModelRecord(model: AbstractModel, data: Record<string, any>): ModelRecord

Defined in: src/typescript/lib/data/ModelRecord.ts:40

Constructs a ModelRecord with the given model schema and initial data.

Parameters

model

AbstractModel

The AbstractModel that describes this record's field schema.

data

Record<string, any>

The initial field values keyed by field name.

Returns

ModelRecord

Methods

commit()

ts
commit(): this

Defined in: src/typescript/lib/data/ModelRecord.ts:124

Accepts current field values as the new baseline, clearing dirty and new flags.

Returns

this

Remarks

Called automatically by AbstractStore.sync() after a successful create or update so that the record no longer appears in subsequent sync cycles.


get()

ts
get(field: string): any

Defined in: src/typescript/lib/data/ModelRecord.ts:53

Returns the value of a field by name.

Parameters

field

string

The logical name of the field to retrieve.

Returns

any

The current value of the field, or undefined if the field is not present.


getData()

ts
getData(): Record<string, any>

Defined in: src/typescript/lib/data/ModelRecord.ts:88

Returns a shallow copy of all field data.

Returns

Record<string, any>

A plain object containing all current field values keyed by field name.


getId()

ts
getId(): any

Defined in: src/typescript/lib/data/ModelRecord.ts:149

Returns the value of the model's primary-key field, or undefined if none is defined.

Returns

any

The primary key value, or undefined if the model has no primary key configured.


getModel()

ts
getModel(): AbstractModel

Defined in: src/typescript/lib/data/ModelRecord.ts:160

Returns the AbstractModel that describes this record's schema.

Returns

AbstractModel

The model instance associated with this record.


isDirty()

ts
isDirty(): boolean

Defined in: src/typescript/lib/data/ModelRecord.ts:97

Returns true if any field has been changed since the last commit.

Returns

boolean

True if the record has uncommitted changes, false otherwise.


isNew()

ts
isNew(): boolean

Defined in: src/typescript/lib/data/ModelRecord.ts:106

Returns true if this record has not yet been persisted (added via store.add).

Returns

boolean

True if the record is new and has not been synced to the server.


markAsNew()

ts
markAsNew(): void

Defined in: src/typescript/lib/data/ModelRecord.ts:113

Marks the record as newly created and not yet synced to the server.

Returns

void


reject()

ts
reject(): void

Defined in: src/typescript/lib/data/ModelRecord.ts:139

Reverts all field values to the last committed state.

Returns

void

Remarks

The dirty flag is cleared but the new flag is not changed; a new record that has been rejected remains new until it is committed or removed from the store.


set()

ts
set(field: string, value: any): this

Defined in: src/typescript/lib/data/ModelRecord.ts:63

Sets a field value and marks the record as dirty.

Parameters

field

string

The logical name of the field to update.

value

any

The new value to assign to the field.

Returns

this