@jimka/typescript-ui / core / Binding
Class: Binding
Defined in: src/typescript/lib/core/Binding.ts:57
Synchronises a ModelRecord with a set of UI components.
Components are registered with bind, either as Bindable implementors (short form) or via explicit accessor callbacks (long form). Once a record is loaded via setRecord, every bound component is populated from the record's fields. Conversely, whenever a component's value changes, the record is updated automatically.
Examples
const binding = new Binding()
.bind('name', nameField)
.bind('active', activeCheckbox)
.bind('role', roleCombo);
binding.setRecord(record);const binding = new Binding()
.bind('name', myWidget, {
get: () => myWidget.getValue(),
set: (v) => myWidget.setValue(v),
listen: (fn) => myWidget.addChangeListener(fn),
});Extends
Constructors
new Binding()
new Binding(): BindingDefined in: src/typescript/lib/core/BaseObject.ts:15
Returns
Inherited from
Methods
addBeforeRecordListener()
addBeforeRecordListener(fn: BeforeRecordListener): voidDefined in: src/typescript/lib/core/Binding.ts:268
Registers a listener that is consulted before a record change takes effect.
The listener receives the next record (or null) and returns false to cancel the change. Iteration stops on the first false; if any listener vetoes, setRecord returns without modifying any state. Returning true — or anything other than false, including undefined — allows the change.
Parameters
fn
Called with the next record (or null). Return false to veto.
Returns
void
Remarks
Async confirmation must be handled at the call site — setRecord stays synchronous. A caller that needs to reconcile its UI after a veto (e.g. reset a record-picker combo) should compare getRecord after the call. A listener that itself calls setRecord re-enters the same veto loop, which is supported but discouraged. null is a valid next value (it represents a clear); a listener that only wants to guard non-clear switches must short-circuit next === null itself.
addChangeListener()
addChangeListener(fn: (fieldName: string, value: unknown) => void): voidDefined in: src/typescript/lib/core/Binding.ts:230
Registers a listener that fires whenever any bound component changes a field value.
Parameters
fn
(fieldName: string, value: unknown) => void
Called with the field name and new value on every change.
Returns
void
addCommitListener()
addCommitListener(fn: () => void): voidDefined in: src/typescript/lib/core/Binding.ts:237
Registers a listener that fires after commit is called.
Parameters
fn
() => void
Returns
void
addRejectListener()
addRejectListener(fn: () => void): voidDefined in: src/typescript/lib/core/Binding.ts:244
Registers a listener that fires after reject is called.
Parameters
fn
() => void
Returns
void
addValidation()
addValidation(
fieldName: string,
component: Component,
rules:
| ValidationRule
| ValidationRule[]): thisDefined in: src/typescript/lib/core/Binding.ts:283
Attaches one or more validation rules to a bound field.
Parameters
fieldName
string
The field name registered with bind.
component
The Component instance to wrap with a FieldDecorator on error.
rules
One or more ValidationRule objects to apply to this field.
ValidationRule | ValidationRule[]
Returns
this
this, for chaining.
bind()
Call Signature
bind<T>(fieldName: string, component: Bindable<T>): thisDefined in: src/typescript/lib/core/Binding.ts:76
Registers a field binding using a component that implements Bindable.
Type Parameters
• T
Parameters
fieldName
string
The record field to bind to.
component
Bindable<T>
A component implementing the Bindable interface.
Returns
this
Call Signature
bind<T>(
fieldName: string,
component: object,
accessors: BindingAccessors<T>): thisDefined in: src/typescript/lib/core/Binding.ts:86
Registers a field binding using explicit accessor callbacks. Use this overload for components that do not implement Bindable.
Type Parameters
• T
Parameters
fieldName
string
The record field to bind to.
component
object
Any object (used only as a placeholder for the overload).
accessors
Getter, setter, and change-listener callbacks.
Returns
this
clearValidation()
clearValidation(): thisDefined in: src/typescript/lib/core/Binding.ts:359
Clears all error decorations without re-running rules. Called automatically by reject.
Returns
this
commit()
commit(): thisDefined in: src/typescript/lib/core/Binding.ts:192
Commits the current record, clearing its dirty and new flags. Fires all registered commit listeners.
Returns
this
getClassName()
getClassName(): stringDefined in: src/typescript/lib/core/BaseObject.ts:44
Returns the runtime class name of this object.
Returns
string
The name of the constructor function as a string.
Inherited from
getId()
getId(): stringDefined in: src/typescript/lib/core/BaseObject.ts:24
Returns the unique identifier for this object.
Returns
string
The UUID string assigned at construction time.
Inherited from
getRecord()
getRecord(): null | ModelRecordDefined in: src/typescript/lib/core/Binding.ts:182
Returns the currently bound record, or null if none is loaded.
Returns
null | ModelRecord
getValidateOnChange()
getValidateOnChange(): booleanDefined in: src/typescript/lib/core/Binding.ts:351
Returns whether live validation is globally enabled.
Returns
boolean
true if live validation is active.
reject()
reject(): voidDefined in: src/typescript/lib/core/Binding.ts:207
Rejects all changes on the current record and re-syncs every component from the reverted field values. Fires all registered reject listeners. Also clears any active validation error decorations.
Returns
void
removeValidation()
removeValidation(fieldName: string): thisDefined in: src/typescript/lib/core/Binding.ts:303
Removes all validation rules for a field and clears any existing error decoration.
Parameters
fieldName
string
The field whose validation should be removed.
Returns
this
this, for chaining.
setId()
setId(id: string): thisDefined in: src/typescript/lib/core/BaseObject.ts:33
Sets the unique identifier for this object.
Parameters
id
string
The new identifier string to assign.
Returns
this
Inherited from
setRecord()
setRecord(record: null | ModelRecord): thisDefined in: src/typescript/lib/core/Binding.ts:153
Loads a record into the binding. All registered components are immediately populated with the corresponding field values. Pass null to clear the binding.
Before any state mutation, every listener registered via addBeforeRecordListener is consulted; if any returns false the call is a complete no-op (no validation reset, no field population) and this is returned unchanged.
Parameters
record
The record to bind, or null to detach.
null | ModelRecord
Returns
this
setValidateOnChange()
setValidateOnChange(enabled: boolean): thisDefined in: src/typescript/lib/core/Binding.ts:340
Enables or disables live validation (validation on every field change).
Parameters
enabled
boolean
true to validate on every change event; false for explicit-only.
Returns
this
unbind()
unbind(fieldName: string): thisDefined in: src/typescript/lib/core/Binding.ts:129
Removes the binding for the given field. The change listener previously attached to the component becomes a no-op.
Parameters
fieldName
string
The field whose binding should be removed.
Returns
this
validate()
validate(): booleanDefined in: src/typescript/lib/core/Binding.ts:321
Runs all registered validation rules against the current field values. Applies or clears FieldDecorator error state for each validated field.
Returns
boolean
true if all fields pass; false if any field fails.