# Actions
The schema allows you to define actions.
There are two main topics when talking about actions.

1. Actions
2. Process

Actions are execution of logic where a process is the sequential execution of several actions.

# Schema Examples

```json
{
    "actions": [
        {
            "id": 0,
            "action": "context.functionToPerform",
            "parameters": {
                "id": 10
            }
        }
    ]
}
```

Action 0 is a example where you want to perform an action on the view model of the screen that uses the schema.
In this case the function name is "functionToPerform" and when calling this function the parameters that will be passed on is the parameters object.
This parameters object contain as many properties as you like.

A implementation of functionToPerform could look like this:

```js
class ViewModel {
    functionToPerform(args) {
        console.log(args.id)
    }
}
```

The important part around actions is to remember that the execution context is the pragma-from rendering the schema.
When executing a path like "context.functionToPerform" this is always assuming pragma-form as the starting point.

If you want to perform a function on pragma-forms itself, you can do something like this:

```json
{
    "actions": [
        {
            "id": 1,
            "action": "setVariable",
            "parameters": {
                "name": "variable",
                "value": "model.dialyRate",
            }
        }
    ]
}

```

# Remote Actions

Some actions you want to perform are remote actions you want to execute on the server.
The pragma forms api does not know what server you are connected to or the requirements of how to call it.
To call these functions you need to mark them as being remote by adding the remote action name to the action definition.


```json
{
    "actions": [
        {
            "id": 2,
            "action": "RemoteFunctionName",
            "remote": "ResourceName",
            "parameters": {
                "id": 10
            }
        }
    ]
}
```

In Onkey development the remote key will normally be the resource name that the action resides on.

Some actions have UI that you want to show before the action is executed.
In such cases you will need two things.

1. Tempate
2. Mode

The template is there to define how the UI should look.
The model serves two purposes, the first is to define any rules you may need and the second is providing a storage space for the edited information.
The dataset definition is the same as any other screen you may have done, the only difference is how you access it.

```json
{
    "id": 1,
    "action": "ResetMeter",
    "remote": "Meters",
    "template": 2,
    "model": "testModel",
    "parameters": {
        "model": "@models.testModel"
    }
}
```

For this action definition to work we need to define the model and declare the model in our variables.

```json
    "variables": {
        "models": {
            "testModel": 1
        }
    },

    "datasets": [
        {
            "id": 1,
            "name": "testModel",
            "fields": [
                {
                    "name": "field1",
                    "default": "Hello"
                },
                {
                    "name": "field2",
                    "default": "World"
                },
                {
                    "name": "field3",
                    "default": true
                },
                {
                    "name": "color",
                    "default": "#FFBB00"
                }
            ]
        }
    ],
```

When declaring datasets you want to instanciate, that instance should be in the varialbes/models path.
As you can see from the example above, the data set id is provided, when the model is instances from the dataset the id will be replaced with the actual model.
On the templat we declared above, you can see that we set the model property to the name of the model we declared in the varialbes.

When accessing the model in the template you can reference it like any other variable.

```json
{
    "element": "input",
    "title": "Field 1",
    "field": "@models.testModel.field1",
    "attribute": {
        "type": "text"
    }
}
```

You will need to send the model back to the performAction function and to do that you need to add it to the parameters

```json
    "parameters": {
        "model": "@models.testModel"
    }
```