docs/docs/en/runjs/context/resource.md
The FlowResource instance in the current context; used to access and operate on data. In most blocks (form, table, detail, etc.) and popups, the runtime binds ctx.resource; in JSBlock and similar contexts that have no resource by default, call ctx.initResource() first, then use ctx.resource.
Use whenever RunJS needs structured data (list, single record, custom API, SQL). Form, table, detail blocks and popups usually have it bound; in JSBlock, JSField, JSItem, JSColumn, etc., call ctx.initResource(type) first if you need to load data.
resource: FlowResource | undefined;
ctx.resource is that instance.undefined by default; after ctx.initResource(type) it is set.Different resource types (MultiRecordResource, SingleRecordResource, APIResource, SQLResource) expose slightly different APIs; common ones:
| Method | Description |
|---|---|
getData() | Current data (list or single record) |
setData(value) | Set local data |
refresh() | Refetch with current params |
setResourceName(name) | Set resource name (e.g. 'users', 'users.tags') |
setFilterByTk(tk) | Set primary key filter (single get, etc.) |
runAction(actionName, options) | Call any resource action (e.g. create, update) |
on(event, callback) / off(event, callback) | Subscribe/unsubscribe (e.g. refresh, saved) |
MultiRecordResource: getSelectedRows(), destroySelectedRows(), setPage(), next(), previous(), etc.
ctx.initResource('MultiRecordResource');
ctx.resource.setResourceName('users');
await ctx.resource.refresh();
const rows = ctx.resource.getData();
const rows = ctx.resource?.getSelectedRows?.() || [];
for (const row of rows) {
console.log(row);
}
await ctx.resource.destroySelectedRows();
ctx.message.success(ctx.t('Deleted'));
ctx.initResource('SingleRecordResource');
ctx.resource.setResourceName('users');
ctx.resource.setFilterByTk(1);
await ctx.resource.refresh();
const record = ctx.resource.getData();
await ctx.resource.runAction('create', { data: { name: 'John' } });
ctx.resource is set.ctx.resource. Use when you need multiple resources or a temporary one.undefined and you must call ctx.initResource first.ctx.resource?.refresh(), especially in JSBlock and similar contexts.setResourceName(name) then refresh() to load data.