ctx.blockModel

The parent block model (BlockModel instance) where the current JS Field / JS Block is located. In scenarios such as JSField, JSItem, and JSColumn, ctx.blockModel points to the form block or table block carrying the current JS logic. In a standalone JSBlock, it may be null or the same as ctx.model.

Scenarios

ScenarioDescription
JSFieldAccess the form, collection, and resource of the parent form block within a form field to implement linkage or validation.
JSItemAccess the resource and collection information of the parent table/form block within a sub-table item.
JSColumnAccess the resource (e.g., getSelectedRows) and collection of the parent table block within a table column.
Form Actions / FlowEngineAccess form for pre-submission validation, resource for refreshing, etc.

Note: ctx.blockModel is only available in RunJS contexts where a parent block exists. In standalone JSBlocks (without a parent form/table), it may be null. It is recommended to perform a null check before use.

Type Definition

blockModel: BlockModel | FormBlockModel | TableBlockModel | CollectionBlockModel | DataBlockModel | null;

The specific type depends on the parent block type: form blocks are mostly FormBlockModel or EditFormModel, while table blocks are mostly TableBlockModel.

Common Properties

PropertyTypeDescription
uidstringUnique identifier of the block model.
collectionCollectionThe collection bound to the current block.
resourceResourceThe resource instance used by the block (SingleRecordResource / MultiRecordResource, etc.).
formFormInstanceForm Block: Ant Design Form instance, supporting getFieldsValue, validateFields, setFieldsValue, etc.
emitterEventEmitterEvent emitter, used to listen for formValuesChange, onFieldReset, etc.

Relationship with ctx.model and ctx.form

RequirementRecommended Usage
Parent block of the current JSctx.blockModel
Read/Write form fieldsctx.form (equivalent to ctx.blockModel?.form, more convenient in form blocks)
Model of the current execution contextctx.model (Field model in JSField, Block model in JSBlock)

In a JSField, ctx.model is the field model, and ctx.blockModel is the form or table block carrying that field; ctx.form is typically ctx.blockModel.form.

Examples

Table: Get selected rows and process

const rows = ctx.blockModel?.resource?.getSelectedRows?.() || [];
if (rows.length === 0) {
  ctx.message.warning('Please select data first');
  return;
}

Form Scenario: Validate and Refresh

if (ctx.blockModel?.form) {
  await ctx.blockModel.form.validateFields();
  await ctx.blockModel.resource?.refresh?.();
}

Listen for Form Changes

ctx.blockModel?.emitter?.on?.('formValuesChange', (payload) => {
  // Implement linkage or re-rendering based on the latest form values
});

Trigger Block Re-render

ctx.blockModel?.rerender?.();

Notes

  • In a standalone JSBlock (without a parent form or table block), ctx.blockModel may be null. It is recommended to use optional chaining when accessing its properties: ctx.blockModel?.resource?.refresh?.().
  • In JSField / JSItem / JSColumn, ctx.blockModel refers to the form or table block carrying the current field. In a JSBlock, it may be itself or an upper-level block, depending on the actual hierarchy.
  • resource only exists in data blocks; form only exists in form blocks. Table blocks typically do not have a form.
  • ctx.model: The model of the current execution context.
  • ctx.form: Form instance, commonly used in form blocks.
  • ctx.resource: Resource instance (equivalent to ctx.blockModel?.resource, use directly if available).
  • ctx.getModel(): Get other block models by UID.