AppML Controllers and Actions

Controllers are the server-side layer that takes a named action - list, insert, update, or delete - coming from the model and actually carries it out against the data source.

What a Controller Does

When appml.js calls a model's action, that call has to be handled somewhere on the server: something has to run a query, insert a row, or apply an update. That handling logic is the controller. The browser and the template never see it directly - they only ever ask for an action by name, and the controller is what makes that name do something real against the data source.

  • list - retrieve records, optionally filtered or sorted, and return them as JSON
  • insert - take submitted field values and add a new record
  • update - take submitted field values plus a record identifier and modify an existing record
  • delete - take a record identifier and remove that record

Example: a form wired to an insert action

<div appml-data="model.json">
  <form appml-action="insert">
    <input name="firstname" placeholder="First name">
    <input name="lastname" placeholder="Last name">
    <input name="department" placeholder="Department">
    <button type="submit">Add Employee</button>
  </form>

  <div appml-repeat="employees">
    <p>{{firstname}} {{lastname}} - {{department}}</p>
  </div>
</div>

Mapping Actions to Logic

Each action named in a model points to the controller logic responsible for it - a query to run for list, a statement to execute for insert/update/delete. Because the model only exposes a small, fixed vocabulary of action names, the same page pattern (a form plus a list) works the same way across completely different tables: the controller behind each action is what changes, not how the template talks to it.

ActionTypical triggerController responsibility
listPage load, or a refresh after another action completesQuery the data source and return matching records
insertA form submission for a new recordValidate submitted fields and add a new record
updateA form submission for an existing recordValidate submitted fields and modify the identified record
deleteA delete button/link for a specific recordRemove the identified record
Note: Because the controller is the only code that actually touches the data source, it's also the right place for validation and permission checks - the browser can be asked to submit only certain fields, but only the controller can enforce that it actually does.

Put together, this is the full loop: a template's appml-data points at a model, the model names an action, the controller behind that action does the real work, and the JSON it returns flows back into {{field}} placeholders and appml-repeat blocks in the page.

Exercise: AppML Controllers

What is the main role of a controller in an AppML app?