Initializes the resource for the current context. If ctx.resource does not already exist, it creates one of the specified type and binds it to the context; if it already exists, it is used directly. Afterward, it can be accessed via ctx.resource.
Generally used in JSBlock (independent block) scenarios. Most blocks, popups, and other components have ctx.resource pre-bound and do not require manual calls. JSBlock has no resource by default, so you must call ctx.initResource(type) before loading data via ctx.resource.
| 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 does not exist; returns the existing one if it does. Ensures ctx.resource is available. |
ctx.makeResource(type) | Only creates and returns a new instance, does not write to ctx.resource. Suitable for scenarios requiring multiple independent resources or temporary use. |
ctx.resource is already pre-bound by the runtime environment, so calling ctx.initResource is unnecessary.setResourceName(name) to specify the collection, and then call refresh() to load the data.ctx.resource