feat(engine): Document Lowdefy action functions in JsAction.

This commit is contained in:
SamTolmay 2021-08-16 14:09:27 +02:00
parent 7af4442c6f
commit 7634145286
No known key found for this signature in database
GPG Key ID: 655CB3F5AA745CF8
2 changed files with 53 additions and 6 deletions

View File

@ -30,19 +30,31 @@ _ref:
description: |
The `JsAction` action is used to call a custom JavaScript function which was loaded onto the page using the `window.lowdefy.registerJsAction()` method. This JavaScript function can be asynchronous. See [Custom Code](/custom-code) for more details on how to register a new JavaScript action.
The returned result of the JavaScript function is accessible through the [`_actions`](/_actions) operator for subsequent action in the event action list.
The returned result of the JavaScript function is accessible through the [`_actions`](/_actions) operator for subsequent actions in the event action list.
#### JsAction function parameters
A `JsAction` function is called with a context object which includes all [`context` data objects](/context-and-state) as well as the list of `args` passed to the action.
A `JsAction` is called with a context object which includes all [`context` data objects](/context-and-state) as well as the list of `args` passed to the action.
```text
(context: {
user: object,
actions: object,
contextId: string,
global: object,
input: object,
pageId: string,
requests: object,
state: object,
urlQuery: object,
input: object,
user: object,
},
...args?: any[]): any
```
#### Using Lowdefy actions in a JsAction
The context passed to the custom JsAction function contains an object called `actions`. This object contains all the Lowdefy action functions like `SetState`, `Request`, and `CallMethod`. The functions can be called using the same parameters as when they are used directly in the Lowdefy configuration. Operators in these parameters are not evaluated.
params: |
###### object
- `name: string`: __Required__ - The registered name of the JavaScript function to call when the action is triggered.
@ -232,3 +244,38 @@ _ref:
}
}
```
##### Use Lowdefy SetState action:
```js
// file: /public/theAnswer.js
function theAnswer({actions}) {
// Think for a while...
return actions.SetState({ answer: 42 })
}
export default theAnswer;
```
##### Use Lowdefy Request action to loop over a list of requests:
```js
// file: /public/loopRequests.js
function loopRequests({ actions }, ...requestIds) {
return Promise.all(requestIds.map((id) => actions.Request(id)));
}
export default loopRequests;
```
```yaml
id: call_all_requests
type: Button
events:
onClick:
- id: loop_requests
type: JsAction
params:
name: loopRequests
args:
- request_1
- request_2
- request_3
```

View File

@ -43,11 +43,11 @@ async function JsAction({ context, event, params, arrayIndices, blockId }) {
return context.lowdefy.imports.jsActions[params.name](
{
...serializer.copy({
user: context.lowdefy.user,
global: context.lowdefy.lowdefyGlobal,
input: context.lowdefy.inputs[context.id],
state: context.state,
urlQuery: context.lowdefy.urlQuery,
input: context.lowdefy.inputs[context.id],
user: context.lowdefy.user,
}),
actions,
contextId: context.id,