docs/docs/en/runjs/context/init-resource.md
Initializes the current context’s resource: if ctx.resource does not exist, creates one of the given type and binds it; otherwise uses the existing one. After that you can use ctx.resource.
Typically used only in JSBlock (standalone block). Most blocks and popups already have ctx.resource; JSBlock does not, so call ctx.initResource(type) first, then use ctx.resource.
initResource(
type: 'APIResource' | 'SingleRecordResource' | 'MultiRecordResource' | 'SQLResource'
): FlowResource;
| Parameter | Type | Description |
|---|---|---|
type | string | Resource type: 'APIResource', 'SingleRecordResource', 'MultiRecordResource', 'SQLResource' |
Returns: The resource instance in the current context (i.e. ctx.resource).
| Method | Behavior |
|---|---|
ctx.initResource(type) | Creates and binds if ctx.resource is missing; otherwise returns existing. Ensures ctx.resource is set |
ctx.makeResource(type) | Creates a new instance and returns it; does not set ctx.resource. Use when you need multiple resources or a temporary one |
ctx.initResource('MultiRecordResource');
ctx.resource.setResourceName('users');
await ctx.resource.refresh();
const rows = ctx.resource.getData();
ctx.render(<pre>{JSON.stringify(rows, null, 2)}</pre>);
ctx.initResource('SingleRecordResource');
ctx.resource.setResourceName('users');
ctx.resource.setFilterByTk(1);
await ctx.resource.refresh();
const record = ctx.resource.getData();
ctx.initResource('MultiRecordResource');
ctx.resource.setDataSourceKey('main');
ctx.resource.setResourceName('orders');
await ctx.resource.refresh();
ctx.resource is already bound; no need to call ctx.initResource.setResourceName(name) and then refresh() to load data.ctx.resource